五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

為何要小心Nginx的add_header指令詳解

發(fā)布日期:2022-03-15 13:08 | 文章來源:腳本之家

大家都知道,nginx配置文件通過使用add_header指令來設置response header。

昨天無聊用curl查看一個站點的信息,發(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
...

主站點在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。除了常規(guī)的header,僅出現(xiàn)了一個配置配置在location中的header X-Cache。

第一印象是CDN過濾了這些header?于是找Cloudflare的文檔,沒發(fā)現(xiàn)會對這些進行處理。轉念一想,CDN過濾這些干啥?。砍燥柫藫蔚陌??他們又不搞zheng審那一套!

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

注意重點在“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或坑。但深入體會這句話,會發(fā)現(xiàn)更有意思的現(xiàn)象:僅最近一處的add_header起作用。http、server和location三處均可配置add_header,但起作用的是最接近的配置,往上的配置都會失效。

但問題還不僅于此。如果location中rewrite到另一個location,最后結果僅出現(xiàn)第二個的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指令。

參考

  • Nginx Module ngx_http_headers_module
  • Nginx add_header configuration pitfall
  • Be very careful with your add_header in Nginx! You might make your site insecure
  • add_header directives in location overwriting add_header directives in server
  • nginx 配置之 add_header 的坑

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對本站的支持。

版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

相關文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部