14

使用此配置:

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

此配置工作正常,但它不符合我的要求:

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

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

4

2 回答 2

8

内部位置尝试这样的事情

# default header value in a new variable
set $esb "$remote_addr, $host";

# if my custom header exists
if ($http_my_header){
  set $esb "$http_my_header, $remote_addr, $host";
}

proxy_set_header my-header $esb;
于 2017-11-09T16:51:31.973 回答
1

与 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/HttpCoreModule#Variables。你想在这里达到什么目的?

于 2013-05-12T04:02:00.313 回答