加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 21:00:21 所属栏目:Nginx 来源:网络整理
导读:使用此配置: 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] "

使用此配置:

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.你想在这里实现什么?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读