Nginx 的 location 實現了對請求的細分處理,有些 URI 返回靜態內容,有些分發到後端服務器等,今天來完全弄懂它的匹配規則nginx
一個最簡單的 location 的例子以下git
server {
server_name website.com;
location /admin/ {
# The configuration you place here only applies to
# http://website.com/admin/
}
}
複製代碼
location 支持的語法 location [=|~|~*|^~|@] pattern { ... }
,乍一看還挺複雜的,來逐個看一下。web
server {
server_name website.com;
location = /abcd {
[…]
}
}
複製代碼
http://website.com/abcd
匹配http://website.com/ABCD
可能會匹配 ,也能夠不匹配,取決於操做系統的文件系統是否大小寫敏感(case-sensitive)。ps: Mac 默認是大小寫不敏感的,git 使用會有大坑。http://website.com/abcd?param1¶m2
匹配,忽略 querystringhttp://website.com/abcd/
不匹配,帶有結尾的/
http://website.com/abcde
不匹配server {
server_name website.com;
location ~ ^/abcd$ {
[…]
}
}
複製代碼
^/abcd$
這個正則表達式表示字符串必須以/
開始,以$
結束,中間必須是abcd
正則表達式
http://website.com/abcd
匹配(徹底匹配)http://website.com/ABCD
不匹配,大小寫敏感http://website.com/abcd?param1¶m2
匹配http://website.com/abcd/
不匹配,不能匹配正則表達式http://website.com/abcde
不匹配,不能匹配正則表達式server {
server_name website.com;
location ~* ^/abcd$ {
[…]
}
}
複製代碼
http://website.com/abcd
匹配 (徹底匹配)http://website.com/ABCD
匹配 (大小寫不敏感)http://website.com/abcd?param1¶m2
匹配http://website.com/abcd/
不匹配
,不能匹配正則表達式http://website.com/abcde
不匹配
,不能匹配正則表達式##「^~」修飾符:前綴匹配 若是該 location 是最佳的匹配,那麼對於匹配這個 location 的字符串, 該修飾符再也不進行正則表達式檢測。注意,這不是一個正則表達式匹配,它的目的是優先於正則表達式的匹配後端
當有多條 location 規則時,nginx 有一套比較複雜的規則,優先級以下:bash
=
^~
(馬上中止後續的正則搜索)~
或~*
這個規則大致的思路是服務器
先精確匹配,沒有則查找帶有
^~
的前綴匹配,沒有則進行正則匹配,最後才返回前綴匹配的結果(若是有的話)app
若是上述規則很差理解,能夠看下面的僞代碼(很是重要) less
function match(uri):
rv = NULL
if uri in exact_match:
return exact_match[uri]
if uri in prefix_match:
if prefix_match[uri] is '^~':
return prefix_match[uri]
else:
rv = prefix_match[uri] // 注意這裏沒有 return,且這裏是最長匹配
if uri in regex_match:
return regex_match[uri] // 按文件中順序,找到即返回
return rv
複製代碼
一個簡化過的Node.js
寫的代碼以下curl
function ngx_http_core_find_location(uri, static_locations, regex_locations, named_locations, track) {
let rc = null;
let l = ngx_http_find_static_location(uri, static_locations, track);
if (l) {
if (l.exact_match) {
return l;
}
if (l.noregex) {
return l;
}
rc = l;
}
if (regex_locations) {
for (let i = 0 ; i < regex_locations.length; i ++) {
if (track) track(regex_locations[i].id);
let n = null;
if (regex_locations[i].rcaseless) {
n = uri.match(new RegExp(regex_locations[i].name));
} else {
n = uri.match(new RegExp(regex_locations[i].name), "i");
}
if (n) {
return regex_locations[i];
}
}
}
return rc;
}
複製代碼
server {
server_name website.com;
location /doc {
return 701; # 用這樣的方式,能夠方便的知道請求到了哪裏
}
location ~* ^/document$ {
return 702; # 用這樣的方式,能夠方便的知道請求到了哪裏
}
}
curl -I website.com:8080/document
HTTP/1.1 702
複製代碼
按照上述的規則,第二個會有更高的優先級
server {
server_name website.com;
location /document {
return 701;
}
location ~* ^/document$ {
return 702;
}
}
curl -I website.com:8080/document
複製代碼
第二個匹配了正則表達式,優先級高於第一個普通前綴匹配
server {
server_name website.com;
location ^~ /doc {
return 701;
}
location ~* ^/document$ {
return 702;
}
}
curl http://website.com/document
HTTP/1.1 701
複製代碼
第一個前綴匹配^~
命中之後不會再搜尋正則匹配,因此會第一個命中
server {
server_name website.com;
location /docu {
return 701;
}
location /doc {
return 702;
}
}
複製代碼
curl -I website.com:8080/document
返回 HTTP/1.1 701
,
server {
server_name website.com;
location /doc {
return 702;
}
location /docu {
return 701;
}
}
複製代碼
curl -I website.com:8080/document
依然返回 HTTP/1.1 701
前綴匹配下,返回最長匹配的 location,與 location 所在位置順序無關
server {
listen 8080;
server_name website.com;
location ~ ^/doc[a-z]+ {
return 701;
}
location ~ ^/docu[a-z]+ {
return 702;
}
}
複製代碼
curl -I website.com:8080/document
返回 HTTP/1.1 701
把順序換一下
server {
listen 8080;
server_name website.com;
location ~ ^/docu[a-z]+ {
return 702;
}
location ~ ^/doc[a-z]+ {
return 701;
}
}
複製代碼
curl -I website.com:8080/document
返回 HTTP/1.1 702
正則匹配是使用文件中的順序,找到返回