为 HAProxy 响应设置标头

man*_*ana 6 haproxy

1.5 版中的新http-response add-header指令是该指令。正如文档中所写的那样- 这可以用于frontend,backendlisten

我在一个frontend部分中尝试做的事情:

use_backend some_backend                if some_condition
http-response add-header Vary Origin    if some_condition
Run Code Online (Sandbox Code Playgroud)

但这没有效果。放置在该backend部分中的同一行工作得很好。有人可以帮助我理解,我在这里缺少什么吗?我不明白,为什么这在该frontend部分不起作用。

My guess would be that within the fronted there is no response yet and so the directive fails. I tried using it before and after die use_backend directive.

Luc*_*oed 6

haproxy -d -V -f haproxy.cfg使用您的示例在前台 ( ) 中运行 haproxy显示以下警告:

acl 'some_condition' will never match because it only involves keywords that are incompatible with 'frontend http-response header rule'
Run Code Online (Sandbox Code Playgroud)

摆弄我的配置,我发现使用此 ACL 时会显示此警告:

acl some_condition req.hdr(Content-Type) -i application/json
Run Code Online (Sandbox Code Playgroud)

但在使用此 ACL 时则不然:

acl some_condition res.hdr(Content-Type) -i application/json
Run Code Online (Sandbox Code Playgroud)

区别在于resvs. req,因此检查响应标头而不是请求标头。这让我相信 a 中的http-response header规则frontend无法检查request标题。

我不知道这是否是 HAProxy 的预期功能,但肯定没有明确记录。