配置 – 为什么我不能将proxy_set_header放在if子句中?

前端之家收集整理的这篇文章主要介绍了配置 – 为什么我不能将proxy_set_header放在if子句中?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用此配置:

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子句中?

提前致谢!

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

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

context: http,server,location

不知道$request变量是什么.它没有出现在Nginx变量列表中:http://wiki.nginx.org/HttpCoreModule#Variables.你想在这里实现什么?

原文链接:https://www.f2er.com/nginx/434775.html

猜你在找的Nginx相关文章