天天看点

nginx反向代理proxy_set_header自定义header头无效的问题nginx反向代理proxy_set_header自定义header头无效

###案例1

环境nginx,linux,tomcat

域名访问是走nginx给后端服务器处理的,

问题是域名经过nginx访问直接不能获取到headers,直接tomcat访问可以

那么问题肯定在nginx上无法处理headers的问题了, 经过查询上面资料得到是nginx的锅,hearders有下划线的锅,nginx设置underscores_in_headers on,参照上面配置说。就可以处理,

测试:

<a href="http://apistore.baidu.com/astore/toolshttpproxy" target="_blank">http://apistore.baidu.com/astore/toolshttpproxy</a>

API工具:GET

<a href="http://www.poxiaoyuan.com/api/user/info.json?user_id=10000050&amp;ouser_id=10000051" target="_blank">http://www.***.com/api/user/info.json?user_id=10000050&amp;ouser_id=10000051</a>

选Headers

Headers  user_id   888888888888888888

tomcat日志下可获取url=/api/user/info.json?user_id=10000050&amp;amp;ouser_id=10000051

para_user_id=10000050

head_user_id=888888888888888888 

不改underscores_in_headers on之前 head_user_id=null

后来找相关资料才得知都是文件头带下划线的锅:

参考资料:

<a href="http://blog.csdn.net/wx_mdq/article/details/10466891" target="_blank">nginx 做proxy 不转发 http header问题解决</a>

<a href="http://blog.csdn.net/wx_mdq/article/details/10466891" target="_blank">http://blog.csdn.net/wx_mdq/article/details/10466891</a>

使用nginx做负载均衡或http代理时,碰到http header不转发的问题。

配置里只有转发设置原始ip和host的

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header     X-Forwarded-Server $host;

但我自定义的header却都忽略掉了。百思不得其解:

1、理论上转发header是基本功能啊,apache都没问题

2、网上也搜不到此类问题说明

3、可能大家都不用客户端自定义提交header?

malcolm说nginx是七层,可能忽略了。让我用其他的代理服务器

可想用nginx,应用又必须自定义http header。咋办?

打开nginx的debug:

配置中:

daemon off;

error_log logs/error.log debug;

看error_log

发现解析header时出现:

2010/12/13 18:49:06 [info] 6248#1476: *1 client sent invalid header line: “wiz_api_version: 2″ while reading client request headers, client: 223.254.100.103, server: localhost, request: “POST /wizkm/a/upload HTTP/1.1″

明显是忽略掉了我自定义的header。

差点绝望。还好开源软件可以看源码。

找出个所以然,果然被我找到问题了:

rc = ngx_http_parse_header_line(r, r-&gt;header_in, cscf-&gt;underscores_in_headers);

 if (r-&gt;invalid_header &amp;&amp; cscf-&gt;ignore_invalid_headers)

在ngx_http_parse_header_line() 函数中

if (ch == ‘_’) {

                if (allow_underscores) {

                    hash = ngx_hash(hash, ch);

                    r-&gt;lowcase_header[i++] = ch;

                    i &amp;= (NGX_HTTP_LC_HEADER_LEN – 1);

                } else {

                    r-&gt;invalid_header = 1;

                }

红色部分就是问题所在了

原来是对header name的字符做了限制,默认 underscores_in_headers 为off,表示如果header name中包含下划线,则忽略掉。

恰好我自定义的header中都是用的下划线。

处理办法:

1:配置中http部分 增加underscores_in_headers on; 配置

2:用减号-替代下划线符号_,避免这种变态问题。nginx默认忽略掉下划线可能有些原因。

踏破铁鞋无处觅  折腾了一天,终于算是解决了。

参考资料:

例子如下:

设置代理服务器ip头

1

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

然后自己在自定义个header,remote_header_test,如下:

proxy_set_header remote_header_test "123123123";

源代码是简单的phpinfo

2

3

4

5

&lt;?php

phpinfo();

?&gt;

在phpinfo结果页面中搜索刚才设置的头部,发现没有找到,网上查找资料,才发现原来nginx会忽略掉下划线的头部变量.于是改成如下:

proxy_set_header remoteheadertest "123123123";

再次打开www.ttlsa.com/nginx_header.php,搜索remoteheadertest,有内容. 看来果真不能用下划线. 然后改成'-',如下:

proxy_set_header remote-header-test "123123123";

打开页面,搜索到的头部是remote_header_test. 自动转换成下划线了.

如果想要支持下划线的话,需要增加如下配置:

underscores_in_headers on;

可以加到http或者server中

语法:underscores_in_headers on|off

默认值:off

使用字段:http, server

是否允许在header的字段中带下划线

本文转自 holy2009 51CTO博客,原文链接:http://blog.51cto.com/holy2010/1840481

继续阅读