徹底弄懂 Nginx location 匹配

Nginx 的 location 實(shí)現(xiàn)了對(duì)請(qǐng)求的細(xì)分處理,有些 URI 返回靜態(tài)內(nèi)容,有些分發(fā)到后端服務(wù)器等,今天來徹底弄懂它的匹配規(guī)則
一個(gè)最簡單的 location 的例子如下

    server_name website.com;
    location /admin/ {
    # The configuration you place here only applies to
    # http://website.com/admin/
    }
}

location 支持的語法 location [=|~|~*|^~|@] pattern { ... },乍一看還挺復(fù)雜的,來逐個(gè)看一下。

location修飾符類型

「=」 修飾符:要求路徑完全匹配
    server_name website.com;
    location = /abcd {
    […]
    }
}

http://website.com/abcd匹配
http://website.com/ABCD可能會(huì)匹配 ,也可以不匹配,取決于操作系統(tǒng)的文件系統(tǒng)是否大小寫敏感(case-sensitive)。ps: Mac 默認(rèn)是大小寫不敏感的,git 使用會(huì)有大坑。
http://website.com/abcd?param1&param2匹配,忽略 querystring
http://website.com/abcd/不匹配,帶有結(jié)尾的/
http://website.com/abcde不匹配

「~」修飾符:區(qū)分大小寫的正則匹配
server {
    server_name website.com;
    location ~ ^/abcd$ {
    […]
    }
}

^/abcd$這個(gè)正則表達(dá)式表示字符串必須以/開始,以$結(jié)束,中間必須是abcd

http://website.com/abcd匹配(完全匹配)
http://website.com/ABCD不匹配,大小寫敏感
http://website.com/abcd?param1&param2匹配
http://website.com/abcd/不匹配,不能匹配正則表達(dá)式
http://website.com/abcde不匹配,不能匹配正則表達(dá)式

「~*」不區(qū)分大小寫的正則匹配
server {
    server_name website.com;
    location ~* ^/abcd$ {
    […]
    }
}

http://website.com/abcd匹配 (完全匹配)
http://website.com/ABCD匹配 (大小寫不敏感)
http://website.com/abcd?param1&param2匹配
http://website.com/abcd/ 不匹配,不能匹配正則表達(dá)式
http://website.com/abcde 不匹配,不能匹配正則表達(dá)式

「^~」修飾符:前綴匹配

如果該 location 是最佳的匹配,那么對(duì)于匹配這個(gè) location 的字符串, 該修飾符不再進(jìn)行正則表達(dá)式檢測。注意,這不是一個(gè)正則表達(dá)式匹配,它的目的是優(yōu)先于正則表達(dá)式的匹配

查找的順序及優(yōu)先級(jí)

當(dāng)有多條 location 規(guī)則時(shí),nginx 有一套比較復(fù)雜的規(guī)則,優(yōu)先級(jí)如下:

精確匹配 =
前綴匹配 ^~(立刻停止后續(xù)的正則搜索)
按文件中順序的正則匹配 ~或~*
匹配不帶任何修飾的前綴匹配。

這個(gè)規(guī)則大體的思路是

先精確匹配,沒有則查找?guī)в?^~的前綴匹配,沒有則進(jìn)行正則匹配,最后才返回前綴匹配的結(jié)果(如果有的話)

如果上述規(guī)則不好理解,可以看下面的偽代碼(非常重要)

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

一個(gè)簡化過的Node.js寫的代碼如下

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;
}

案例分析
案例 1

server {
    server_name website.com;
    location /doc {
        return 701; # 用這樣的方式,可以方便的知道請(qǐng)求到了哪里
    }
    location ~* ^/document$ {
        return 702; # 用這樣的方式,可以方便的知道請(qǐng)求到了哪里

    }
}

curl -I  website.com:8080/document
HTTP/1.1 702

按照上述的規(guī)則,第二個(gè)會(huì)有更高的優(yōu)先級(jí)
案例2

server {
    server_name website.com;
    location /document {
        return 701;
    }
    location ~* ^/document$ {
        return 702;
    }
}
curl -I  website.com:8080/document

第二個(gè)匹配了正則表達(dá)式,優(yōu)先級(jí)高于第一個(gè)普通前綴匹配
案例 3

server {
    server_name website.com;
    location ^~ /doc {
        return 701;
    }
    location ~* ^/document$ {
        return 702;
    }
}
curl http://website.com/document
HTTP/1.1 701

第一個(gè)前綴匹配^~命中以后不會(huì)再搜尋正則匹配,所以會(huì)第一個(gè)命中
案例 4

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 所在位置順序無關(guān)
案例 5

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
正則匹配是使用文件中的順序,找到返回

鏈接:https://juejin.im/post/5ce5e1f65188254159084141

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容