HAProxy community

Sending HAProxy hostname as Header?

I’ve a feeling I’m missing something obvious here but I can’t seem to work out how to add the HAProxy hostname as a HTTP Header.

Scenario is that I’ve a pair of HAProxy 2.0 servers running on Debian in Azure. They’re not in a cluster, Azure Load Balancer is handling the front end HA and the application database is handling the statefulness so I don’t need them to be aware of each other (I think).

I’ve no major need to know the HAProxy hostname, I’m not using it for any purpose other than troubleshooting but it seems odd that there doesn’t seem to be a straightforward way to add this as a host header (which makes me think I’m missing something obvious)

I’m familiar with the ‘http-send-name-header’ which can send the target server name and obviously I could add a manual header to each HAProxy server’s individual config but I’m keen to keep the config standard across all front end servers.

Any thoughts welcome

Something like documented in the env keyword documentation?

# Pass the Via header to next hop with the local hostname in it
http-request add-header Via 1.1\ %[env(HOSTNAME)]

Unfortunately I’d already tried that and it doesn’t appear to work. However your response has moved me further along as it gave me the confidence I was looking in the right area :slight_smile: .

I did some additional searching and it seems that other people have noted that HAProxy doesn’t appear to have access to environment variables when running as a service.
See the link above for what appears to be the reason in the answer from NotPeter

I addressed the issue by adding the line


to the file


then adding your code to my HAProxy config

http-request set-header X-HAProxy-Hostname %[env(HAPROXY_HOSTNAME)]

This works fine.
Still leaves me with a per-server config but that single file is easy to modify as a once off as part of the server deployment script.

Thanks for the pointer

If it’s a bash script, you should be able to just do:


You could also put this in init.d scripts, or if systemd is what you use, you can probably set a environment variable even more easily (systemd can access the hostname via the %H variable).

Actually we have a hostname sample, this should be very easy:

http-request set-header X-HAProxy-Hostname hostname
1 Like

As a wise man once said… Doh!

I knew I had to be missing something simple. Many thanks. Minor tweak needed

http-request set-header X-HAProxy-Hostname %[hostname]

1 Like