nginx反向代理默認行爲
- Nginx在做反向代理時,一般會設置host和ip,如果你的請求的headers裏有值,它是可以同時轉發過去的
Nginx反向代理時headers無效的問題
-
有的時候我們會發現反向代理時有的header獲取不到 , 原因是 : 默認情況下,並不是所有
headers
的fields
它都會轉發,fields
裏帶有下劃線(_)
的,Nginx視爲不合法,自動拋棄
不發了。例如如下:access_token: this is your token
-
官方文檔的解釋:
Syntax: underscores_in_headers on | off;
Default: underscores_in_headers off;
Context: http, server
Enables or disables the use of underscores in client request header fields. When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.文檔中說明 : 想要支持下劃線(
_
)的headers fields
,就需要將underscores_in_headers
設置爲on
; 如果只是設置它,會發現,並沒用,因爲還要設置一項proxy_pass_request_headers on;
-
完整的解決方案如下:
server { ... underscores_in_headers on; location / { proxy_pass_request_headres on; proxy_pass http://server; } }