Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache ProxyPass with dynamic hostname

I'm trying to use Apache as a gateway to reverse proxy to a backend server with the same name as the requested http_host.

ex:

    ProxyPass            /  https://%{HTTP_HOST}/
    ProxyPassReverse     /  https://%{HTTP_HOST}/

I'm getting an error when I use this setup. Suggestions?

like image 290
brakertech Avatar asked Sep 08 '25 00:09

brakertech


1 Answers

There's no way to dynamically reverse proxy like that using proxy pass. However, you can do it using mod_rewrite's P flag. The same thing with ProxyPassReverse, you can't use the %{HTTP_HOST}, however, since the hostnames are the same as the same, you don't need it at all. Just need:

RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [L,P]

One issue you may run into is that since DNS resolves proxying server to some IP, the proxying server must know that the same DNS hostname does not resolve to itself and actually resolves to a backend server (the server to proxy to), otherwise it will cause a loop.

like image 136
Jon Lin Avatar answered Sep 09 '25 13:09

Jon Lin