8

Consider the following HAProxy Config:

frontend front
        default_backend default

backend default
        balance roundrobin
        http-response set-header X-RGN us-east-1
        server app-1a app.us-east-1a.example.com:443 ssl verify none check
        server app-1c app.us-east-1c.example.com:443 ssl verify none check
        server app-1b app.us-east-1b.example.com:443 ssl verify none check

I would like to return a response header the indicates the server that was chosen. For example, if the frontend receives a request, it will balance roundrobin and forward the request to a backend server, when it responds, I would like to see in my browser which server was used.

The config might look something like this:

frontend front
        default_backend default

backend default
        balance roundrobin
        http-response set-header X-RGN us-east-1
        server app-1a app.us-east-1a.example.com:443 ssl verify none check
        server app-1c app.us-east-1c.example.com:443 ssl verify none check
        server app-1b app.us-east-1b.example.com:443 ssl verify none check
        http-response set-header X-Server app-1a if server -i app-1a
        http-response set-header X-Server app-1b if server -i app-1b
        http-response set-header X-Server app-1c if server -i app-1c

Has anyone tried this before?

Ace
  • 463
  • 3
  • 6
  • 16

1 Answers1

21

Assuming HAProxy 1.5 or later:

http-response set-header X-Server %s
Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
  • This doesnt seem to work for me, my `haproxy -v` returns as `HA-Proxy version 1.6.3 2015/12/25`. The header does not come back when i use `curl -v`. – Ace Mar 30 '17 at 02:10
  • Strange, I took it (I thought...) from a live 1.6 server configuration. Let me verify. – Michael - sqlbot Mar 30 '17 at 04:36
  • Yes, works for me in 1.6.9. I still had builds of 1.6.1 and 1.6.4 on that machine, so I stopped the running 1.6.9 and started up the old 1.6.1 & 1.6.4 and it works, on both of those, as well -- I see it in `curl -v` output. It would be strange if there's a regression like this in 1.6.3, so I wonder... did you reload config? Did you remove the other `set-header` configuration for the same header name (assuming it was ever there)? Also, if you're behind an ELB/ALB or CloudFront, HAProxy soft reload will leave some kept-alive spare connections talking to the old process for a short time. – Michael - sqlbot Mar 30 '17 at 05:02
  • thanks for looking into this. This is on a standard ec2 instance, I have restarted the service and I still dont see it working, here is my config: https://gist.github.com/anonymous/3f962cdb0e3d6d2d151ae90c881f5980 – Ace Mar 30 '17 at 21:17
  • nevermind, this seems to be working fine today, i mustve missed something when testing it earlier. – Ace Mar 30 '17 at 21:43
  • @Sairam thanks. I've updated the answer to say 1.5. It's been a while since I wrote this, so I'm not sure, now, why I originally specified 1.6, since `http-response set-header` was introduced prior to the release of 1.5.0. – Michael - sqlbot Feb 27 '20 at 03:46