Nginx的add_header指令實(shí)例分析

前言

大家都知道,nginx配置文件通過(guò)使用add_header指令來(lái)設(shè)置response header。

用curl查看一個(gè)站點(diǎn)的信息,發(fā)現(xiàn)返回的頭部與想象中的不一樣:

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 ...

主站點(diǎn)在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";

但響應(yīng)頭部沒(méi)有這些header。除了常規(guī)的header,僅出現(xiàn)了一個(gè)配置配置在location中的header x-cache。

第一印象是cdn過(guò)濾了這些header?于是找cloudflare的文檔,沒(méi)發(fā)現(xiàn)會(huì)對(duì)這些進(jìn)行處理。轉(zhuǎn)念一想,cdn過(guò)濾這些干啥啊?吃飽了撐的啊?他們又不搞zheng審那一套!

問(wèn)題轉(zhuǎn)移到nginx的配置上。打開(kāi)google搜索”nginx location add_header”,果然發(fā)現(xiàn)不少槽點(diǎn)。點(diǎn)開(kāi)官網(wǎng)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.

注意重點(diǎn)在“these directives are inherited from the previous level if and only if there are no add_header directives defined on the current level. ”。即:僅當(dāng)當(dāng)前層級(jí)中沒(méi)有add_header指令才會(huì)繼承父級(jí)設(shè)置。所以我的疑問(wèn)就清晰了:location中有add_header,nginx.conf中的配置被丟棄了。

這是nginx的故意行為,說(shuō)不上是bug或坑。但深入體會(huì)這句話(huà),會(huì)發(fā)現(xiàn)更有意思的現(xiàn)象:僅最近一處的add_header起作用。http、server和location三處均可配置add_header,但起作用的是最接近的配置,往上的配置都會(huì)失效。

但問(wèn)題還不僅于此。如果location中rewrite到另一個(gè)location,最后結(jié)果僅出現(xiàn)第二個(gè)的header。例如:

location?/foo1?{ ?add_header?foo1?1; ?rewrite?/?/foo2; }  location?/foo2?{ ?add_header?foo2?1; ?return?200?"ok"; }

不管請(qǐng)求/foo1還是/foo2,最終header只有foo2:

Nginx的add_header指令實(shí)例分析

盡管說(shuō)得通這是正常行為,但總讓人感覺(jué)有點(diǎn)勉強(qiáng)和不舒坦:server丟掉http配置,location丟掉server配置也就算了,但兩個(gè)location在同一層級(jí)啊!

不能繼承父級(jí)配置,又不想在當(dāng)前塊重復(fù)指令,解決辦法可以用include指令。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊15 分享