jml*_*lrt 11 cache http-headers haproxy
我在 tomcat 服务器前使用 HAproxy 1.6 作为负载平衡器。
我需要根据请求 URI 添加响应标头。
例如,Cache-Control public,max-age="600"
当请求 uri 是/api
但不是当请求 uri 是其他东西时,我想添加响应头。
我的第一次尝试是使用基于路径的 acl 将标头添加到 http-response:
acl api path_reg ^/api/(.*)$
http-response add-header Cache-Control public,max-age="600" if api
Run Code Online (Sandbox Code Playgroud)
当我用 开始haproxy 时-d
,我有警告说path_reg
(或path
)与http-response
以下不兼容:
Dec 6 15:22:29 ip-10-30-0-196 haproxy-systemd-wrapper[315]:
[WARNING] 340/152229 (2035) : parsing
[/etc/haproxy/haproxy.cfg:78] : acl 'api' will never match because
it only involves keywords that are incompatible with 'backend
http-response header rule'
Run Code Online (Sandbox Code Playgroud)我试图添加标题http-request
而不是http-response
:
acl api path_reg ^/api/(.*)$
http-request add-header Cache-Control public,max-age="600" if api
Run Code Online (Sandbox Code Playgroud)
那行得通,但我在响应中需要它
我还尝试使用 haproxy 变量:
http-request set-var(txn.path) path
acl path_acl %[var(txn.path)] -m ^/api/(.*)$
http-response add-header Cache-Control public,max-age="600" if path_acl
Run Code Online (Sandbox Code Playgroud)
但是当我尝试 HAproxy 时没有事件启动,并且出现以下错误:
[ALERT] 340/162647 (2241) : parsing [/etc/haproxy/haproxy.cfg:48]
: error detected while parsing ACL 'path_acl' : unknown fetch
method '%[var' in ACL expression '%[var(txn.path)]'.
Run Code Online (Sandbox Code Playgroud)如何使用请求路径进入 acl 来设置响应标头?
Mic*_*bot 10
尝试这个:
http-response set-header Cache-Control no-cache,\ max-age=600 if { capture.req.uri -m beg /api/ }
Run Code Online (Sandbox Code Playgroud)
capture.req.uri
一直持续到响应被处理,不像path
,它没有。
一些注意事项:
此示例使用匿名 ACL。您也可以使用命名的 ACL 来完成,但这需要 2 行。
我没有理由知道您为什么应该引用 max-age 值。
你可能不想add-header
,你想set-header
,这确保如果一个已经存在,它将被删除。
acl path_acl %[var(txn.path)] -m ^/api/(.*)$
可能正确地写为acl path_acl var(txn.path) -m ^/api/(.*)$
. HAProxy 对何时期望%[ ]
和何时不期望有点挑剔。我确定有一种模式,但我不清楚到底是什么。
归档时间: |
|
查看次数: |
14621 次 |
最近记录: |