婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 為何要小心Nginx的add_header指令詳解

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

熱門標(biāo)簽:百應(yīng)電銷機(jī)器人靠譜嗎 coreldraw地圖標(biāo)注 遼寧電銷平臺外呼系統(tǒng)軟件 營銷電銷機(jī)器人招商 申請400電話號碼多少錢 物超所值的400電話申請 如何在各種地圖標(biāo)注自己的地址 信陽銷售外呼系統(tǒng)招商 菏澤crm外呼系統(tǒng)價(jià)格

前言

大家都知道,nginx配置文件通過使用add_header指令來設(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)頭部沒有這些header。除了常規(guī)的header,僅出現(xiàn)了一個(gè)配置配置在location中的header X-Cache。

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

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

注意重點(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)前層級中沒有add_header指令才會繼承父級設(shè)置。所以我的疑問就清晰了:location中有add_header,nginx.conf中的配置被丟棄了。

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

但問題還不僅于此。如果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";
}

不管請求/foo1還是/foo2,最終header只有foo2:

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

不能繼承父級配置,又不想在當(dāng)前塊重復(fù)指令,解決辦法可以用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 的坑

總結(jié)

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

標(biāo)簽:玉樹 內(nèi)江 湖北 泰安 江門 江西 廈門 瀘州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《為何要小心Nginx的add_header指令詳解》,本文關(guān)鍵詞  為何,要,小心,Nginx,的,add,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《為何要小心Nginx的add_header指令詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于為何要小心Nginx的add_header指令詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 广河县| 江北区| 大化| 祥云县| 海盐县| 麻阳| 盖州市| 湘阴县| 清镇市| 鄂州市| 台中县| 白水县| 濉溪县| 霸州市| 海丰县| 灵寿县| 随州市| 赤城县| 习水县| 乌恰县| 山西省| 大方县| 醴陵市| 青神县| 天峨县| 邢台市| 尼勒克县| 玉田县| 伊通| 资中县| 托里县| 珲春市| 奉节县| 广饶县| 宜阳县| 石柱| 兴安县| 长兴县| 余庆县| 交城县| 澄城县|