###案例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&ouser_id=10000051" target="_blank">http://www.***.com/api/user/info.json?user_id=10000050&ouser_id=10000051</a>
選Headers
Headers user_id 888888888888888888
tomcat日志下可擷取url=/api/user/info.json?user_id=10000050&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->header_in, cscf->underscores_in_headers);
if (r->invalid_header && cscf->ignore_invalid_headers)
在ngx_http_parse_header_line() 函數中
if (ch == ‘_’) {
if (allow_underscores) {
hash = ngx_hash(hash, ch);
r->lowcase_header[i++] = ch;
i &= (NGX_HTTP_LC_HEADER_LEN – 1);
} else {
r->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
<?php
phpinfo();
?>
在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