Apache: Dynamic Reverse Proxy

Solution 1:

You can use the ProxyPassMatch directive like this:

ProxyPassMatch /([^/]*)/(.*)$ https://$1.example.com/$2

However, you will get partial results.

The proxy part will work, however, the cookie path will be different: as you request /host1, and the backend server sets cookies to /, this can lead to problems. Also, redirects aren't handled properly: if the target host redirects to an absolute url, e.g. sets a Location: http://... header to itself, then the proxy won't rewrite that header, making the browser follow the direct link.

There is a solution for both the cookie and the redirect problems, using the ProxyPassReverse, ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath directives, but to my knowledge, none of them supports regexp or any kind of dynamic mappings. Of course, you can specify them one by one, but this makes the whole dynamic proxying idea pointless.