天天看點

nginx conf location 配置

http://eyesmore.iteye.com/blog/1141660

比對 規則是:       1 先比對普通location (再比對正規表達式)。      注意:官方文檔這句話就明确說了,先普通location ,而不是有些同學的誤區“先比對正則location ”      2 “ 普通location ”内部(普通location 與普通location )是如何比對的呢?簡單的說: 最大字首比對。

     3    普通location比對不代表是最終結果,還會繼續比對正則location的比對: 普通location 先比對,而且選擇了 最大字首比對後,不能就停止後面的比對,最大字首比對隻是一個臨時的結果,            nginx 還需要繼續檢查正則location (但至于最終才能普通location 的最大字首比對,還是正則location 的比對,截止目前的内容還沒講,但後面會講)。

     4  “正則location ”與“正則location ”内部的比對規則是:按照正則location 在配置檔案中的 實體順序(編輯順序)比對的(這句話就說明location 并不是一定跟順序無關,               隻是普通location 與           順序無關,正則location 還是與順序有關的),并且隻要 比對到一條正則location ,就 不再考慮後面的(這與“普通location ”與“正則location ”之間的規則不一樣             “普通location ”與“正則location ”之間的規則是:選擇出“普通location ”的最大字首比對結果後,還需要繼續搜尋正則location )

     5 “普通location ”的最大字首比對結果與繼續搜尋的“正則location ”比對結果的決策關系。如果繼續搜尋的“正則location ”也有比對上的,            那麼“ 正則location ”覆寫 “普通location ”的最大字首比對(因為有這個覆寫關系,是以造成有些同學以為正則location 先于普通location 執行的錯誤了解);            但是如果“ 正則location ”沒有能比對上,那麼就用“普通location ”的最大字首比對結果

     6   隻比對普通location 不比對 正則location(^~ 不要繼續比對正則 ):通常的規則是,比對完了“普通location ”指令,還需要繼續比對“正則location ”,          但是你也可以告訴Nginx :比對到了“普通location ”後,不再需要繼續比對“正則location ”了,          要做到這一點隻要在“普通location ”前面加上“^~ ”符号(^ 表示“非”,~ 表示“正則”,字元意思是:不要繼續比對正則)           除了上文的“^~ ”可以阻止繼續搜尋正則location 外,你還可以加 “= ”。           

那麼如果“^~ ”和“= ”都能阻止繼續搜尋正則location 的話,那它們之間有什麼差別呢?差別很簡單,共同點是它們都能阻止繼續搜尋正則location ,不同點是“^~ ”依然遵守“最大字首”比對規則,然而“= ”不是“最大字首”,而是必須是嚴格比對(exact match )。 這裡順便講下“location / {} ”和“location = / {} ”的差別,“location / {} ”遵守普通location 的最大字首比對,由于任何 URI 都必然以“/ ”根開頭,是以對于一個URI ,如果有更specific 的比對,那自然是選這個更specific 的,如果沒有,“/ ”一定能為這個URI 墊背(至少能比對到“/ ”),也就是說“location / {} ”有點預設配置的味道,其他更specific 的配置能覆寫overwrite 這個預設配置(這也是為什麼我們總能看到location / {} 這個配置的一個很重要的原因)。而“location = / {} ”遵守的是“嚴格精确比對exact match ”,也就是隻能比對 http://host:port/ 請求,同時會禁止繼續搜尋正則location 。是以如果我們隻想對“GET / ”請求配置作用指令,那麼我們可以選“location = / {} ”這樣能減少正則location 的搜尋,是以效率比“location / {}” 高(注:前提是我們的目的僅僅隻想對“GET / ”起作用)。

    7     隐式禁止繼續搜尋正則location : On exact match with literal location without "=" or "^~" prefixes search is also immediately terminated. 前面我們說了,普通location 比對完後,還會繼續比對正則location ;但是nginx 允許你阻止這種行為,方法很簡單,隻需要在普通location 前加“^~ ”或“= ”。但其實還有一種“隐含”的方式來阻止正則location 的搜尋,這種隐含的方式就是:當“最大字首”比對恰好就是一個“嚴格精确(exact match )”比對,照樣會停止後面的搜尋。原文字面意思是:隻要遇到“精确比對exact match ”,即使普通location 沒有帶“= ”或“^~ ”字首,也一樣會終止後面的比對。 先舉例解釋下,後面例題會用實踐告訴大家。假設目前配置是:location /exact/match/test.html { 配置指令塊1} ,location /prefix/ { 配置指令塊2} 和 location ~ \.html$ { 配置指令塊3} ,如果我們請求 GET /prefix/index.html ,則會被比對到指令塊3 ,因為普通location /prefix/ 依據最大比對原則能比對目前請求,但是會被後面的正則location 覆寫;當請求GET /exact/match/test.html ,會比對到指令塊1 ,因為這個是普通location 的exact match ,會禁止繼續搜尋正則location 。

     小結:To summarize, the order in which directives are checked is as follows: 1 Directives with the "=" prefix that match the query exactly. If found, searching stops. 2 All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops. 3 Regular expressions, in the order they are defined in the configuration file. 4 If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.

“正則 location ”讓步 “普通 location ”的嚴格精确比對結果;但覆寫 “普通 location ”的最大字首比對結果。

“普通 location ”與“正則 location ”的比對規則:先比對普通 location ,再比對正則 location , 但是如果普通 location 的比對結果恰好是“嚴格精确( exact match )”的,則 nginx 不再嘗試後面的正則 location ; 如果普通 location 的比對結果是“最大字首”,則正則 location 的比對覆寫普通 location 的比對。 也就是前面說的“正則 location 讓步普通 location 的嚴格精确比對結果,但覆寫普通 location 的最大字首比對結果”

     8  “@ ”是用來定義“Named Location ”的(你可以了解為獨立于“普通location (location using literal strings )”和“正則location (location using regular expressions )”之外的第三種類型), 這種“Named Location ”不是用來處理普通的HTTP 請求的,它是專門用來處理“内部重定向(internally redirected )”請求的。注意:這裡說的“内部重定向(internally redirected )”或許說成“forward ”會好點,以為内internally redirected 是不需要跟浏覽器互動的,純粹是服務端的一個轉發行為。

繼續閱讀