HAProxy - Add response header to indicate the server that was chosen
Asked Answered
D

1

10

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?

Doughty answered 29/3, 2017 at 23:16 Comment(0)
S
24

Assuming HAProxy 1.5 or later:

http-response set-header X-Server %s
Suppositive answered 30/3, 2017 at 1:4 Comment(6)
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.Doughty
Strange, I took it (I thought...) from a live 1.6 server configuration. Let me verify.Suppositive
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.Suppositive
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: gist.github.com/anonymous/3f962cdb0e3d6d2d151ae90c881f5980Doughty
nevermind, this seems to be working fine today, i mustve missed something when testing it earlier.Doughty
@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.Suppositive

© 2022 - 2024 — McMap. All rights reserved.