4

I have a group of linux apache 1.3 servers behind a load balancer, and I want to be able to, at a glance, determine which server I'm hitting. The load balancer is severely limited in its monitoring capabilities, so what I'd like to do is configure apache to send an additional header indicating the machine's hostname.

I know I could just hard-code a header into the httpd.conf with the hostname:

Header set X-Which-Host-Am-I 'host1'

However, all the servers in question are mirrored with rsync, including the configs, so hard-coding the hostnames is out. Is there a way I can call the hostname command and dump it into a header?

Clarification: Since multiple virtual hosts live on these servers, I want the physical machine's hostname, not the domain name in the request.

sysadmin1138
  • 133,124
  • 18
  • 176
  • 300
EvanK
  • 247
  • 3
  • 7
  • 13

4 Answers4

10

You need to pass an Environment Variable using mod_env:

PassEnv HOSTNAME
Header set X-Hostname "%{HOSTNAME}e"

You can set the value of HOSTNAME through the envvars file (mine is /etc/apache2/envvars)

Also, if you're using PHP you can use environment variables

anarcat
  • 752
  • 1
  • 9
  • 18
hdanniel
  • 4,293
  • 23
  • 25
2

Untested, but how about passing the line in via the init (or other startup) script? Something like:

/path/to/httpd -c "Header set X-Hostname $HOSTNAME"

If you're using a RH-flavour distro you might be able to squeeze this into /etc/sysconfig/httpd or similar to avoid editing the init script.

markdrayton
  • 2,449
  • 1
  • 20
  • 24
1

I had this same issue and I solved it using the PassEnv Apache config directive which imports an environment variable from the system shell into the Apache config environment.

PassEnv HOSTNAME
Header set X-Which-Host-Am-I "%{HOSTNAME}e"

Fortunately for me I found that HOSTNAME was already set in my system shell environment. You can check what's available in your shell environment using printenv on the command line.

So as long as the HOSTNAME environment variable is set differently on each server you can still have identical Apache configurations for all and each will report it's unique identity in the header.

I tested this on CentOS 7 with Apache 2.4. I tried to look up the Apache docs for V1.3 to see if the same directives were available in that version, but it seems that docs before Version 2.2 are no longer available at apache.org.

Sam Kearns
  • 11
  • 1
0

With Apache 2.4.9 on Ubuntu 18.04, this worked for me

nano /etc/apache2/envvars
export HOSTNAME=$(hostname)

nano /etc/apache2/sites-available/default-ssl.conf
<IfModule mod_ssl.c>
        <VirtualHost _default_:443>
                Header set X-Hostname ${HOSTNAME}

What actually ended up working for me and my proxy setup though was to use ProxyVia instead

CircaLucid
  • 121
  • 2
  • 1
    This answer was mainly to help @Adil – CircaLucid Aug 17 '18 at 15:40
  • `ProxyVia Full` just adds a header like this `via: 1.1 www.example.com (Apache/2.4.29)`, with not much information: did you set it with the above `hostname` edit too? – watery Sep 24 '20 at 12:35
  • What I was aiming when I found this thread was to have an internal Apache server provide it's internal hostname after passing through an external facing proxy. ProxyVia Full did exactly that without X-Hostname part. – CircaLucid Sep 27 '20 at 00:02
  • Yep, trying to get the same, but for me `via` didn't contain the server internal hostname, but the requested public host. – watery Sep 28 '20 at 09:22