I have two proxypass rules on my web host, one that points to a local varnish instance for caching purposes, which I want ProxyPreserveHost enabled for, and another that points to a third party hosted site, which I want ProxyPreserveHost disabled for. Is there anyway I can do this on a per rule/pass basis?
3 Answers
Under Apache 2.2, no - the ProxyPreserveHost
directive is only valid in the server config or virtual host contexts; you'd need the different ProxyPass
statements to be in different virtual hosts.
In Apache 2.4, yes - the directory context has been added for the directive, so you can now do something such as:
<Location /to-varnish/>
ProxyPreserveHost On
ProxyPass http://127.0.0.1:8000/to-varnish/
</Location>
<Location /to-third-party/>
ProxyPreserveHost Off
ProxyPass http://third-party-site.com/
</Location>

- 114,520
- 13
- 181
- 251
You can with help of RequestHeader
ProxyPreserveHost On
<LocationMatch third-party-pattern>
RequestHeader set Host third-party-vhost-name
ProxyPassMatch http://third-party-server
</LocationMatch>
<LocationMatch varnish-pattern>
ProxyPassMatch http://varnish-server
</LocationMatch>

- 31
- 1
I'm unable to upvote Sorin's response...
In our local situation running Apache 2.2, and the primary app requires proxypreservehost (CQ/AEM author) to login, but a partner we proxy to requires their host in the host header.
We don't need to do this with regex though, a regular <Location...>
works fine.
From documentation (http://httpd.apache.org/docs/2.2/mod/core.html#location):
The directive limits the scope of the enclosed directives by URL.
This solution works for us:
<Location /[path]/ >
RequestHeader set Host [thirdparty]
</Location>
RewriteRule ^/[path]/(.*) https://[thirdparty]/$1 [P,NC,L]
This sets the host header for this request to the partner's hostname.
With that in place, we can continue to login to CQ/AEM author AND proxy to the partner service using their expected host header.

- 1
- 1