小编Neu*_*ino的帖子

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

使用此配置:

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

重新加载 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
Run Code Online (Sandbox Code Playgroud)

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

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

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

configuration nginx proxy

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

configuration ×1

nginx ×1

proxy ×1