使用 HAProxy 作为负载均衡器时如何根据所选后端设置主机标头

发布于 2025-01-16 19:56:28 字数 2066 浏览 2 评论 0原文

在某些情况下,需要根据 HAProxy 负载均衡选择的后端修改主机标头(示例如下:https://www.claudiokuenzler.com/blog/919/haproxy-how-use- different-http-host-header-for-each-backend-server

我的互联网研究表明,有基本上有两种方法可以实现此目的:

a)使用“http-send-name-header Host”(根据 HAProxy 文档不推荐:https://www.haproxy.com/documentation /hapee/latest/onepage/#4.2-http-send-name-header)

b) 使用“http-request set-header Host ... if { srv_id 1 }”

(以下文章介绍了这两种技术:https://serverfault .com/questions/876871/configure-haproxy-to-include-host-headers-for- Different-backends

选项 a)按预期工作,但不鼓励使用(请参阅https://www.haproxy.com /documentation/hapee/latest/onepage/#4.2-http-send-name-header)。

出于这个原因,我尝试选择 b)。不幸的是,配置胆怯地拒绝工作:

backend loadbalanced-backends

        mode http

        balance roundrobin

        option forwardfor

        http-request set-header Host one.domain.com if { srv_id 1 }

        http-request set-header Host two.domain.com if { srv_id 2 }

        server one one.domain.com:8000

        server two two.domain.com:8000

      

日志中打印的警告:

[WARNING]  (1) : config : parsing ....cfg:14] : anonymous acl will never match because it uses keyword 'srv_id' which is incompatible with 'backend http-request header rule'

[WARNING]  (1) : config : parsing ....cfg:15] : anonymous acl will never match because it uses keyword 'srv_id' which is incompatible with 'backend http-request header rule'

除了警告之外,记录的请求标头显示主机未更改为 one.domain.com 或。两个.domain.com。

警告“匿名 acl 将永远不会匹配...”到底是什么意思?我看不出配置有什么问题。欢迎任何想法。

Under certain circumstances it is required to modify the host-header based on the backend selected by HAProxy loadbalancing (an example is described here: https://www.claudiokuenzler.com/blog/919/haproxy-how-use-different-http-host-header-for-each-backend-server)

My internet research shows that there are basically two ways to achieve this:

a) use "http-send-name-header Host" (not recommended according the HAProxy docs: https://www.haproxy.com/documentation/hapee/latest/onepage/#4.2-http-send-name-header)

b) use " http-request set-header Host ... if { srv_id 1 }"

(the following article describes these two techniques: https://serverfault.com/questions/876871/configure-haproxy-to-include-host-headers-for-different-backends)

Option a) works as expected but it is discouraged to be use (see https://www.haproxy.com/documentation/hapee/latest/onepage/#4.2-http-send-name-header).

For this reason I am trying to option b). Unfortunately the config cowardly refuses to work:

backend loadbalanced-backends

        mode http

        balance roundrobin

        option forwardfor

        http-request set-header Host one.domain.com if { srv_id 1 }

        http-request set-header Host two.domain.com if { srv_id 2 }

        server one one.domain.com:8000

        server two two.domain.com:8000

      

The warnings printed in the log:

[WARNING]  (1) : config : parsing ....cfg:14] : anonymous acl will never match because it uses keyword 'srv_id' which is incompatible with 'backend http-request header rule'

[WARNING]  (1) : config : parsing ....cfg:15] : anonymous acl will never match because it uses keyword 'srv_id' which is incompatible with 'backend http-request header rule'

Besids the warning the logged request-header show that the host is not changed to one.domain.com resp. two.domain.com.

What exactly means the warning "anonymous acl will never match..."? I do not see what is wrong with the configuration. Any ideas are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文