有没有办法检查nginx
中是否存在特定的cookie?
现在,我有一个类似下面的部分来设置cookie的标题:
proxy_set_header x-client-id $cookie_header_x_client_id;
我想检查该cookie是否存在,然后设置标题,否则不要覆盖标题.
我试过了:
if ($cookie_header_x_client_id) {
proxy_set_header x-client-id $cookie_header_x_client_id;
}
但它不起作用,并给出以下错误:
"proxy_set_header" directive is not allowed here in /etc/nginx/sites-enabled/website:45
有什么办法吗?
最佳答案
在nginx的if
上下文中只允许使用有限数量的指令.这与以下事实有关:if是rewrite
模块的一部分;因此,在其上下文中,您只能使用模块文档中明确概述的指令.
解决此“限制”的常见方法是使用中间变量建立状态,然后使用诸如proxy_set_header
这样的中间变量来使用指令:
set $xci $http_x_client_id;
if ($cookie_header_x_client_id) {
set $xci $cookie_header_x_client_id;
}
proxy_set_header x-client-id $xci;