Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 926
Ace Avatar asked Mar 29 '17 23:03

Ace


1 Answers

Assuming HAProxy 1.5 or later:

http-response set-header X-Server %s
like image 105
Michael - sqlbot Avatar answered Sep 21 '22 08:09

Michael - sqlbot