ProxyPreserveHost on individual proxypass rules

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?


Solution 1:

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>

Solution 2:

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>