Search code examples
djangoapacheproxypassapache-config

With Apache's ProxyPassMatch, how do I pass on only part of my URL?


I'm running Apache 2.4 with a Python 3 / Django 3 app, both in Docker containers. If my Apache container gets a request with "/api", I would like to redirect that request to the Python container. So if my Apache request is

http://localhost:9090/api/states/US

I would like to redirect to the Python container using the URL

http://web:8000/states/US

In my virtual host file I have

<VirtualHost *:80>
    ServerName directory.example.com

    ProxyPassMatch    ^/api http://web:8000/(.*)
    ProxyPassReverse  ^/api http://web:8000/(.*)

But when I make the request for "http://localhost:9090/api/states/US", I get this in my Docker logs

maps-web-1       | Not Found: /(.*)/api/states/US
maps-apache-1    | 172.23.0.1 - - [30/Jun/2022:20:23:49 +0000] "GET /api/states/US HTTP/1.1" 404 6128

So evidently my ProxyPassMatch is not correct. How do I set that up properly?


Solution

  • You have missed out quotes by the looks of it. Try something like

    ProxyPassMatch    "/api(.*)" "http://web:8000$1"
    ProxyPassReverse  "/api(.*)" "http://web:8000$1"
    

    In the above the (.*) is a curved bracket selector that finds everything after /api and places it in the $1 position in the next string.