小心Nginx的add_header指令
轉載請註明文章出處:https://tlanyan.me/be-careful-with-nginx-add_header-directive/
昨天無聊用curl
檢視一個站點的資訊,發現返回的頭部與想象中的不一樣:
HTTP/2 200 date: Thu, 07 Feb 2019 04:26:38 GMT content-type: text/html; charset=UTF-8 vary: Accept-Encoding, Cookie cache-control: max-age=3, must-revalidate last-modified: Thu, 07 Feb 2019 03:54:54 GMT X-Cache: Miss server: cloudflare ...
主站點在nginx.conf
中配置了HSTS等header:
add_header Strict-Transport-Security "max-age=63072000; preload"; add_header X-Frame-Options SAMEORIGIN; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block";
但響應頭部沒有這些header。除了常規的header,僅出現了一個配置配置在location
中的headerX-Cache
。
第一印象是CDN過濾了這些header?於是找Cloudflare
的文件,沒發現會對這些進行處理。轉念一想,CDN過濾這些幹啥啊?吃飽了撐的啊?他們又不搞zheng審那一套!
問題轉移到Nginx的配置上。開啟Google搜尋”nginx location add_header”,果然發現不少槽點。點開官網add_header
的文件,有這樣的描述(其他資訊已省略):
There could be several add_header directives. These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level.
注意重點在“These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level. ”
。即:
僅噹噹前層級中沒有add_header
指令才會繼承父級設定
。所以我的疑問就清晰了:location
中有add_header
,nginx.conf
中的配置被丟棄了。
這是Nginx
的故意行為,說不上是bug或坑。但深入體會這句話,會發現更有意思的現象:僅最近一處的add_header
起作用。http
、server
和location
三處均可配置add_header
,但起作用的是最接近的配置,往上的配置都會失效。
但問題還不僅於此。如果location
中rewrite
到另一個location
,最後結果僅出現第二個的header。例如:
location /foo1 { add_header foo1 1; rewrite / /foo2; } location /foo2 { add_header foo2 1; return 200 "OK"; }
不管請求/foo1
還是/foo2
,最終header只有foo2
:
儘管說得通這是正常行為,但總讓人感覺有點勉強和不舒坦:server
丟掉http
配置,location
丟掉server
配置也就算了,但兩個location
在同一層級啊!
不能繼承父級配置,又不想在當前塊重複指令,解決辦法可以用include
指令。