nginx "proxy_set header" directive is not allowed的解决办法

 提示:转载请注明原文链接

 本文链接:https://360us.net/article/93.html

server {

    location / {
        proxy_buffering off;
        proxy_http_version 1.1;
        proxy_read_timeout 36000s;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        if ($subdomain = "plex") {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Forwarded-Host $host;
        }
    }
}

如上配置,nginx会报错:

shell nginx: [emerg] "proxy_set_header" directive is not allowed here in /etc/nginx/nginx.conf:78 

原因是proxy_set_header指令不能放在if块下面,新旧版的nginx都是这样的规则。

如果需要if判断来传递header,可以用下面的方法:

        # 默认值设置为空
        set $x_value "";

        if ($arg_test) {
            set $x_value "present only when test argument set";
        }

        # $x_value不为空的时候X-Header才会被发送
        proxy_set_header X-Header $x_value;

还可以用map指令来保存值。

nginx-trac


本文链接:https://360us.net/article/93.html