最近一段時間在學習 Nginx ,以前一直對 Nginx 的 Location 配置很頭大,最近終于弄出點眉目。總結如下:
nginx 配置檔案,自下到上分為三種層次分明的結構:
| http block the protocol level
| server block the server level
V location block the requested URI
Nginx 允許使用者定義 Location block ,并指定一個比對模式(pattern)比對特定的 URI。除了簡單的字元串(比如檔案系統路徑),還允許使用更為複雜的比對模式(pattern)。
Location block 的基本文法形式是:
location [=|~|~*|^~|@] pattern { ... }
[=|~|~*|^~|@] 被稱作 location modifier ,這會定義 Nginx 如何去比對其後的 pattern ,以及該 pattern 的最基本的屬性(簡單字元串或正規表達式)。
------- 關于 location modifier -------
1. =
這會完全比對指定的 pattern ,且這裡的 pattern 被限制成簡單的字元串,也就是說這裡不能使用正規表達式。
Example:
server {
server_name website.com;
location = /abcd {
[…]
}
}
比對情況:
http://website.com/abcd # 正好完全比對
http://website.com/ABCD # 如果運作 Nginx server 的系統本身對大小寫不敏感,比如 Windows ,那麼也比對
http://website.com/abcd?param1&param2 # 忽略查詢串參數(query string arguments),這裡就是 /abcd 後面的 ?param1&param2
http://website.com/abcd/ # 不比對,因為末尾存在反斜杠(trailing slash),Nginx 不認為這種情況是完全比對
http://website.com/abcde # 不比對,因為不是完全比對
2. (None)
可以不寫 location modifier ,Nginx 仍然能去比對 pattern 。這種情況下,比對那些以指定的 patern 開頭的 URI,注意這裡的 URI 隻能是普通字元串,不能使用正規表達式。
location /abcd {
http://website.com/abcd/ # 末尾存在反斜杠(trailing slash)也屬于比對範圍内
http://website.com/abcde # 仍然比對,因為 URI 是以 pattern 開頭的
3. ~
這個 location modifier 對大小寫敏感,且 pattern 須是正規表達式
location ~ ^/abcd$ {
http://website.com/abcd # 完全比對
http://website.com/ABCD # 不比對,~ 對大小寫是敏感的
http://website.com/abcd/ # 不比對,因為末尾存在反斜杠(trailing slash),并不比對正規表達式 ^/abcd$
http://website.com/abcde # 不比對正規表達式 ^/abcd$
注意:對于一些對大小寫不敏感的系統,比如 Windows ,~ 和 ~* 都是不起作用的,這主要是作業系統的原因。
4. ~*
與 ~ 類似,但這個 location modifier 不區分大小寫,pattern 須是正規表達式
location ~* ^/abcd$ {
http://website.com/ABCD # 比對,這就是它不區分大小寫的特性
5. ^~
比對情況類似 2. (None) 的情況,以指定比對模式開頭的 URI 被比對,不同的是,一旦比對成功,那麼 Nginx 就停止去尋找其他的 Location 塊進行比對了(與 Location 比對順序有關)
6. @
用于定義一個 Location 塊,且該塊不能被外部 Client 所通路,隻能被 Nginx 内部配置指令所通路,比如 try_files or error_page
------- 搜尋順序以及生效優先級 -------
因為可以定義多個 Location 塊,每個 Location 塊可以有各自的 pattern 。是以就需要明白(不管是 Nginx 還是你),當 Nginx 收到一個請求時,它是如何去比對 URI 并找到合适的 Location 的。
要注意的是,寫在配置檔案中每個 Server 塊中的 Location 塊的次序是不重要的,Nginx 會按 location modifier 的優先級來依次用 URI 去比對 pattern ,順序如下:
1. =
2. (None) 如果 pattern 完全比對 URI(不是隻比對 URI 的頭部)
3. ^~
4. ~ 或 ~*
5. (None) pattern 比對 URI 的頭部
----------------------------------------------------------------------------------
注:雖說是總結,其實基本上就是 Nginx HTTP Server 該部分的翻譯……,原文見該書電子版的第四章p133-138