- « Apache: extending mod_remoteip to support Host/Port/Protocol mangling natively
- Apache: adding early client IP and local socket IP options to RequestHeader »
Apache: set canonical name from Host header
Patch description:
When using Apache 2.4 behind a load balancer, sometimes need arises to put some virtual host to some other host path. Like you have host http://user1.domain/, but want to also have this host at http://shared.domain/user1/. While linking /user1/ to host root is no problem with trivial Alias on Apache side and rewrite on the balancer side sending requests to http://user1.domain/user1/ instead of http://shared.domain/user1/, it's not complete because any accesses to this virtual host should have shared.domain Host shown i.e. in SERVER_NAME and not user1.domain. This can be of course be obtained by creating shared.domain virtual host on Apache side and mapping all the user paths to it, but most of the time it is not very appealing solution because it requires duplication of all the users configuration, crafty rewrites, etc. And if user virtual hosts run under user credentials, i.e. with mpm_itk, it may be even impossible to combine them all into single virtual hosts.
This patch adds a possibility to set 'canonical' server name to a value supplied in Host header, also this can be done i.e. after mod_remoteip restores proper Host from some X-Forwarded host. This patch just adds host option to UseCanonicalName directive of Apache server to force it creating a canonical name from Host header *after* virtual host is selected.
Example:
RequestHeader set Host expr=%{req:X-Forwarded-Host} early
or
RemoteHostHeader X-Forwarded-Host
(with another patch of mod_remoteip you can find on this blog)
then
<VirtualHost *>
ServerName user1.domain
UseCanonicalName host
Alias /user1/ /www/user1/docroot/
...
</VirtualHost>
That way, you can send i.e. the following request headers along with /user1/... path for request to http://shared.domain/user1/:
Host: user1.domain
X-Forwarded-Host: shared.domain
And virtual host will be selected to user1.domain user space, while i.e. PHP will see SERVER_NAME as shared.domain.
Download:
httpd-2.4-use-host-as-name.patch
Tested against Apache 2.4.27 in production, compatible with Apache 2.4.28.