如果存在cookie,如何有条件地覆盖nginx中的头? [英] How to conditionally override a header in nginx only if a cookie exist?
问题描述
有没有办法检查 nginx $ c $中是否存在特定的Cookie c>
?
Is there a way to check if a specific cookie exist in nginx
?
现在我有一个类似下面的部分来设置cookie的标题:
For now I have a section like below to set header from 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;
}
但它不起作用并给出以下错误:
But it does not work and gives the error below:
"proxy_set_header" directive is not allowed here in /etc/nginx/sites-enabled/website:45
任何解决方案?
推荐答案
只有 if
nginx 中的上下文。这与如果
是 <$的一部分有关c $ c>重写 模块;因此,在其上下文中,您只能使用模块文档中明确列出的指令。
There are only a limited number of directives that are allowed within an if
context in nginx. This is related to the fact that if
is part of the rewrite
module; as such, within its context, you can only use the directives that are specifically outlined within the documentation of the module.
围绕此限制的常见方法是构建up状态使用中间变量,然后使用指令,如 proxy_set_header
使用这样的中间变量:
The common way around this "limitation" is to build up state using intermediate variables, and then use directives like proxy_set_header
using such intermediate variables:
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;
这篇关于如果存在cookie,如何有条件地覆盖nginx中的头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!