1

is it possible to have an Apache server dynamically proxy all requests defined by a subdir? Like:

https://proxy.example.com/host1           -> https://host1.example.com
https://proxy.example.com/host2           -> https://host2.example.com
https://proxy.example.com/host3/dir1      -> https://host3.example.com/dir1

Bonus:

https://proxy.example.com/host4:8000      -> https://host4.example.com:8000
https://proxy.example.com/host5:8080/dir2 -> https://host5.example.com:8080/dir2

My knowledge is, ahem, limited. Is the following example something that can be expanded?

RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST} [P]

SSLEngine on
SSLProxyEngine On
ProxyRequests Off

ProxyPass            /  https://$1/
ProxyPassReverse     /  https://$1/
ProxyPreserveHost On

Kind regards

1 Answer 1

2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .