Returning server name in HTTP response headers
Tuesday, January 10th, 2006This particular problem has been bugging me for some time now: How does one track down a problematic web server behind a load balancer?
In the past, we’ve hidden HTML elements in the document body to indicate the server that generated the response. (For an example of this, go to a page on friendster and view source, search for "phpapp" and you’ll see one.) This has a couple drawbacks. The server name is not returned if:
- Apache is segfaulting or otherwise fucking around on you.
- You are viewing non-dynamic content such as static HTML.
- You are requesting images
Initially I tried using the PassEnv directive (See Docs) to pass in the $HOSTNAME variable from the system by adding the following to my apache config.
# Note - this config will not work
PassEnv HOSTNAME
Header set hostname %{HOSTNAME}
However, for the life of me, I could not figure out how to make apache properly interpret the HOSTNAME variable. Every time I looked at the response header, I would just get the following header:
hostname: %{HOSTNAME}
If someone reading this knows why this is, please reply and let me know where my error is.
The workaround:
I modified my /etc/init.d/apache start script with the following lines. (You’ll need to modify these a bit depending on your distro.)
HTTPD_OPT="-c header set Hostname $HOSTNAME"
…
startproc -t $HTTPD_START_TIMEOUT $HTTPD_BIN "$HTTPD_OPT
(Don’t forget to enable the mod_headers module in your httpd.conf as well.)
Now, whenever you make a request from the outside, you’ll see a response like this one:
HTTP/1.x 302 Found
Date: Tue, 10 Jan 2006 22:31:09 GMT
Server: Apache/1.3.31 (Unix) PHP/4.3.8
Hostname: phpapp64
X-Powered-By: PHP/4.3.8
Location: /home.php
Cneonction: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
X-Pad: avoid browser bug
If you don’t already have it, I suggest checking out LiveHTTPHeaders for Mozilla FireFox which will display request/response headers as you use your browser.