HAProxy - 添加响应标头以指示所选的服务器

时间:2017-03-29 23:16:04

标签: header http-headers haproxy

考虑以下HAProxy配置:

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

我想返回一个响应标头,表示所选的服务器。例如,如果前端收到请求,它将平衡roundrobin并将请求转发给后端服务器,当它响应时,我想在浏览器中看到使用了哪个服务器。

配置可能如下所示:

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

以前有人试过吗?

1 个答案:

答案 0 :(得分:7)

假设HAProxy 1.6或更高版本:

http-response set-header X-Server %s