nginx反向代理proxy_set_header自定义header头无效
公司使用nginx作为负载均衡,有时候需要自定义header头发送给后端的真实服务器. 想过去应该是非常的简单的事情.
例子如下:
设置代理服务器ip头
| 1 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | 
然后自己在自定义个header,remote_header_test,如下:
| 1 | proxy_set_header remote_header_test "123123123"; | 
接着后端真实服务器打开www.ttlsa.com/nginx_header.php
源代码是简单的phpinfo
| 1 2 3 4 5 | <?php phpinfo(); ?> | 
在phpinfo结果页面中搜索刚才设置的头部,发现没有找到,网上查找资料,才发现原来nginx会忽略掉下划线的头部变量.于是改成如下:
| 1 | proxy_set_header remoteheadertest "123123123"; | 
再次打开www.ttlsa.com/nginx_header.php,搜索remoteheadertest,有内容. 看来果真不能用下划线. 然后改成'-',如下:
| 1 | proxy_set_header remote-header-test "123123123"; | 
打开页面,搜索到的头部是remote_header_test. 自动转换成下划线了.
如果想要支持下划线的话,需要增加如下配置:
| 1 | underscores_in_headers on; | 
可以加到http或者server中
语法:underscores_in_headers on|off
默认值:off
使用字段:http, server
是否允许在header的字段中带下划线
实例配置:
       server_name  dev.fashio*****.cn;
        
     underscores_in_headers on;
     location /bi-api/ {
            proxy_pass http://192.168.0.155:8080/;
            proxy_set_header Host $host;
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号