I use HAproxy 1.6 as load balancer in front of tomcat servers.
I need to add response headers based on the request URI.
For e.g., I'd like to add the response header Cache-Control public,max-age="600"
when the request uri is /api
but not when the request uri is something else.
My first try was using acl based on path to add the headers to http-response:
acl api path_reg ^/api/(.*)$
http-response add-header Cache-Control public,max-age="600" if api
When I start haproxy with -d
, I have warning saying that path_reg
(or path
) is incompatible with 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'
I tried to add the header in http-request
instead of http-response
:
acl api path_reg ^/api/(.*)$
http-request add-header Cache-Control public,max-age="600" if api
That worked but I need it in the response
I also tried to use haproxy variables:
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
But when I try HAproxy doesn't event start and I have the following error:
[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)]'.
How can I use the request path into an acl to set the response header?
Попробуйте следующее:
http-response set-header Cache-Control no-cache,\ max-age=600 if { capture.req.uri -m beg /api/ }
capture.req.uri
сохраняется до обработки ответа, в отличие от path
, который этого не делает.
Несколько примечаний :
В этом примере используется анонимный ACL. Вы также можете сделать это с помощью именованного ACL, но это займет 2 строки.
Я не знаю причин, по которым вы должны указывать значение max-age.
Вы, вероятно, не хотите add-header
, вы хотите установить заголовок
, который гарантирует, что, если он уже есть, он будет удален.
acl path_acl% [var (txn.path)] -m ^ / api / (. *) $
, вероятно, правильно записано как acl path_acl var (txn.path) -m ^ / api / (. *) $
. HAProxy немного привередлив, когда ожидает % []
, а когда нет. Я уверен, что есть закономерность, но не совсем понимаю, что это такое.