中文字幕一区二区三区在线中文-日本中文字幕 在线观看-欧美日韩国产亚洲综合-性色AV一二三天美传媒

廣州總部電話:020-85564311
20年
互聯(lián)網(wǎng)應(yīng)用服務(wù)商
廣州總部電話:020-85564311
20年
互聯(lián)網(wǎng)應(yīng)用服務(wù)商
請輸入搜索關(guān)鍵詞
知識庫 知識庫

優(yōu)網(wǎng)知識庫

探索行業(yè)前沿,共享知識寶庫

nginx 路徑匹配規(guī)則是怎樣的?

發(fā)布日期:2025-05-14 18:22:53 瀏覽次數(shù): 851 來源:打破砂鍋聊技術(shù)

当一个服务的 nginx 配置路径规则较多时,存在多条规则可以匹配到一个url可能,这种情况下nginx 是如何决定使用哪条规则呢?在配置规则比较多的时候,会经常遇到这个问题。

Location定义规则是怎样的?

nginx 官方文档中语法规则定义如下:


    location [ = | ~ | ~* | ^~ ] uri

    说明:

    1. uri 有两种格式

      a)前缀字符串 prefix string,前缀字符串匹配必须是从开头精确匹配,例如  /my-site/some/path 就不会匹配  /some/path/

      b)正则表达式,正则表达式规范为 Perl-Compatible-Rx。

    2. 匹配符号有五种情况,<空> =^~~~* , 匹配符的术语叫modifier。

    3. 其中,<空>, =^~ 适用于前缀表达式

    4. 另外两种~开头的适用正则表达式, “~” modifier (for case-sensitive matching) 即大小写敏感, “~*” modifier (for case-insensitive matching) 大小写不敏感。

    匹配顺序是怎样的?

    当有多条规则可以匹配到一个url 时,nginx 是如何决定使用哪条规则呢?这就是匹配顺序的问题。

    nginx 官方文档 是这样描述的

    To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations).

    Among them, the location with the longest matching prefix is selected and remembered.

    Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used.

    If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.

    If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.

    Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.

    另一个文档  里这样描述:

    1. Test the URI against all prefix strings.
    2. The = (equals sign) modifier defines an exact match of the URI and a prefix string. If the exact match is found, the search stops.
    3. If the ^~ (caret-tilde) modifier prepends the longest matching prefix string, the regular expressions are not checked.
    4. Store the longest matching prefix string.
    5. Test the URI against regular expressions.
    6. Stop processing when the first matching regular expression is found and use the corresponding location.
    7. If no regular expression matches, use the location corresponding to the stored prefix string.

    总结:

    1. 先检查所有前缀表达式规则(非正则)。
    2. 如果有精确匹配 =规则匹配,使用该规则。
    3. 其余规则中,计算哪一条是“最长匹配”。
    4. 如果最长匹配的这一条 匹配符号是 ^~ 开头 ,则停止匹配并使用该条规则。
    5. 按配置文件顺序检查所有正则规则,遇到匹配的即Stop且使用。
    6. 如果正则规则中没有匹配项,则使用4记录的最长匹配规则。

    能否举个例子?

    这是来自官方文档的一个例子。


      location = / {    [ configuration A ]}
      location / {    [ configuration B ]}
      location /documents/ {    [ configuration C ]}
      location ^~ /images/ {    [ configuration D ]}
      location ~* \.(gif|jpg|jpeg)$ {    [ configuration E ]}
      • The “/” request will match configuration A,  精确匹配
      • the “/index.html” request will match configuration B, 因为仅 B规则匹配。
      • the “/documents/document.html” request will match configuration C。 B和C都匹配,C 最长,且正则均不匹配
      • the “/images/1.gif” request will match configuration D。 B + D,D匹配最长,且是~- 匹配符,Stop,不检查正则。
      • the “/documents/1.jpg” request will match configuration E.  B 和 E匹配,B 没有~- , 需挨个检查正则, E正则匹配,Stop并使用。。


      優(yōu)網(wǎng)科技,優(yōu)秀企業(yè)首選的互聯(lián)網(wǎng)供應(yīng)服務(wù)商

      優(yōu)網(wǎng)科技秉承"專業(yè)團隊、品質(zhì)服務(wù)" 的經(jīng)營理念,誠信務(wù)實的服務(wù)了近萬家客戶,成為眾多世界500強、集團和上市公司的長期合作伙伴!

      優(yōu)網(wǎng)科技成立于2001年,擅長網(wǎng)站建設(shè)、網(wǎng)站與各類業(yè)務(wù)系統(tǒng)深度整合,致力于提供完善的企業(yè)互聯(lián)網(wǎng)解決方案。優(yōu)網(wǎng)科技提供PC端網(wǎng)站建設(shè)(品牌展示型、官方門戶型、營銷商務(wù)型、電子商務(wù)型、信息門戶型、微信小程序定制開發(fā)、移動端應(yīng)用(手機站、APP開發(fā))、微信定制開發(fā)(微信官網(wǎng)、微信商城、企業(yè)微信)等一系列互聯(lián)網(wǎng)應(yīng)用服務(wù)。


      我要投稿

      姓名

      文章鏈接

      提交即表示你已閱讀并同意《個人信息保護聲明》

      專屬顧問 專屬顧問
      掃碼咨詢您的優(yōu)網(wǎng)專屬顧問!
      專屬顧問
      馬上咨詢
      掃一掃馬上咨詢
      掃一掃馬上咨詢

      掃一掃馬上咨詢