nginx:为什么我不能把proxy_set_header放在if子句中?

有了这个configuration

server { listen 8080; location / { if ($http_cookie ~* "mycookie") { proxy_set_header X-Request $request; proxy_pass http://localhost:8081; } } } 

我在重新加载nginx服务时出现此错误:

 Reloading nginx configuration: nginx: [emerg] "proxy_set_header" directive is not allowed here in /etc/nginx/conf.d/check_cookie.conf:5 nginx: configuration file /etc/nginx/nginx.conf test failed 

这个configuration工作正常,但它没有做我想要的:

 server { listen 8080; location / { proxy_set_header X-Request $request; if ($http_cookie ~* "mycookie") { proxy_pass http://localhost:8081; } } } 

为什么我不能把proxy_set_header指令放在if子句中?

假设你实际上是想问,'我怎样才能得到这个工作',那么只要重写,那么标题总是被传递的,但是如果你不想设置,它会被设置为一些被忽略的值。

 server { listen 8080; location / { set $xheader "someignoredvalue"; if ($http_cookie ~* "mycookie") { set $xheader $request; } proxy_set_header X-Request $xheader; if ($http_cookie ~* "mycookie") { proxy_pass http://localhost:8081; } } 

'如果'在nginxconfiguration中通常是不好的。 你可以使用地图模块来使事情工作。 见http://nginx.org/en/docs/http/ngx_http_map_module.html http://wiki.nginx.org/HttpMapModule