Nginx 的 Location 配置指令塊


最近一段時間在學習 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 只能是普通字符串,不能使用正則表達式。
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)也屬於匹配范圍內
    http://website.com/abcde    # 仍然匹配,因為 URI 是以 pattern 開頭的

3. ~
這個 location modifier 對大小寫敏感,且 pattern 須是正則表達式
Example:
server {
    server_name website.com;
    location ~ ^/abcd$ {
    […]
    }
}
匹配情況:
    http://website.com/abcd        # 完全匹配
    http://website.com/ABCD        # 不匹配,~ 對大小寫是敏感的
    http://website.com/abcd?param1&param2    # 忽略查詢串參數(query string arguments),這里就是 /abcd 后面的 ?param1&param2
    http://website.com/abcd/    # 不匹配,因為末尾存在反斜杠(trailing slash),並不匹配正則表達式 ^/abcd$
    http://website.com/abcde    # 不匹配正則表達式 ^/abcd$
注意:對於一些對大小寫不敏感的系統,比如 Windows ,~ 和 ~* 都是不起作用的,這主要是操作系統的原因。

4. ~*
與 ~ 類似,但這個 location modifier 不區分大小寫,pattern 須是正則表達式
Example:
server {
    server_name website.com;
    location ~* ^/abcd$ {
    […]
    }
}
匹配情況:
    http://website.com/abcd        # 完全匹配
    http://website.com/ABCD        # 匹配,這就是它不區分大小寫的特性
    http://website.com/abcd?param1&param2    # 忽略查詢串參數(query string arguments),這里就是 /abcd 后面的 ?param1&param2
    http://website.com/abcd/    # 不匹配,因為末尾存在反斜杠(trailing slash),並不匹配正則表達式 ^/abcd$
    http://website.com/abcde    # 不匹配正則表達式 ^/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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM