为什么我不能把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子句中?

提前致谢!

与proxy_pass不同,您不能将proxy_set_header放在if块中。 你只能把它放在http / server / location块中。 所以你的第二个配置是好的。

参考: http : //nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

上下文:http,服务器,位置

不知道$ request变量是什么。 它不会出现在nginx变量列表中: http : //wiki.nginx.org/HttpCoremodulee#Variables 。 你想在这里做什么?

在里面的位置尝试这样的事情

//新变量中的默认标题值

设置$ esb“$ remote_addr,$ host”;

//如果我的自定义标题存在

如果($ http_my_header){

设置$ esb“$ http_my_header,$ remote_addr,$ host”;

}

proxy_set_header my-header $ esb;