nginx反向代理proxy_set_header自定义head

作者: 怀老师 | 来源:发表于2020-05-27 20:02 被阅读0次

    今天测试在nginx服务器上增加一个header属性,把真实IP通过新属性发回来,下面分享下正确的设置和踩坑记录。

    第一个坑

    首先,是在网上看到了这个设置:

    location /{
       try_files $uri $uri/ /index.php?$query_string;
       proxy_set_header client-real-ip $remote_addr;
    }
    

    在server里设置后,发现不生效。打印$_SERVER数组没有发现我新增的字段。

    这里有个坑,就是所写的属性名不能为下划线连接。比如test-ip可以,test_ip就不行。强行使用需要改nginx的一个属性:underscores_in_headers on;

    第二个坑

    然后我想了想,会不会是作用域的问题,把这行配置,从localtion /挪到了localtion ~.php$中。

    location ~ \.php$ {
                    fastcgi_pass 127.0.0.1:9000;
                    proxy_set_header client-real-ip $remote_addr;
                    proxy_set_header client-real-ip $remote_addr;
           }
    
            location / {
               try_files $uri $uri/ /index.php?$query_string;
            }
    

    成功修改

    这时还是不生效,我开始有点怀疑设置没生效,执行了一遍nginx -s reload
    打印$_SERVER数组,还是查不到这个属性。
    这时,想到了会不会是fastcgi的问题。

        把proxy_set_header client-real-ip $remote_addr;
        替换成
        fastcgi_param client-real-ip  $remote_addr;
    

    重启,生效了。

    相关文章

      网友评论

        本文标题:nginx反向代理proxy_set_header自定义head

        本文链接:https://www.haomeiwen.com/subject/wwhgahtx.html