Apache UseCanonicalName On isnt passing ServerName to CGI

SpliFF asked:

On Apache 2.4 in in a virtualhost I have:

UseCanonicalName On
ServerName somename
ServerAlias www.someothername.com

According to the docs:

With UseCanonicalName On Apache will use the hostname and port
specified in the ServerName directive to construct the canonical name
for the server. This name is used in all self-referential URLs, and
for the values of SERVER_NAME and SERVER_PORT in CGIs.

So in my Tomcat/CFML application when I visit the URL www.someothername.com I would expect to see in the CGI scope:

server_name: somename

but instead I get:

server_name: www.someothername.com

It’s like the directive is totally ignored.

# Proxy CFML files to Tomcat
RewriteCond %{REQUEST_FILENAME} /[^/:]+\.cfml*($|/)
RewriteRule (.*) ajp://%{HTTP_HOST}:8009$1 [P,L]

I also tried:

RewriteRule (.*) ajp://%{SERVER_NAME}:8009$1 [P,L]

and using mod_proxy instead of AJP:

RewriteRule (.*) http://%{SERVER_NAME}:8888$1 [P,L]

The last 2 cause a DNS lookup on somename but still returns www.someothername.com in the CGI.SERVER_NAME field

I should point out that the only reason I’m doing this is because I’m doing mass virtual-hosting with mod_cfml to automaticatically create tomcat contexts and I would like the context and application to use a short name derived from the vhost configuration. I guess I could just set a header (even rewrite the Host: header) but using ServerName seemed the most elegant solution.

UPDATE: There is something I noticed in the client headers that is probably relevant. There are 2 headers I haven’t seen before:

x-forwarded-host: www.someothername.com
x-forwarded-server: somename

I need to know what set these headers and why. I’m assuming it was either Tomcat or mod_cfml. Can I rely on the x-forwarded-server value to always be ServerName?

My answer:

You’re not calling a CGI, you’re proxying, so trying to set SERVER_NAME is either useless or very close to it.

Typically with mass virtual hosting you should use the actual domain name that you received, which is HTTP_HOST, the Host: HTTP header, in your application to make the distinction.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.