日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

目錄
  • 1. 對url的匹配
    • 1.1 默認匹配
    • 1.2 精確匹配( = )
    • 1.3 正則,區分大小寫 ( ~ )
    • 1.4 正則表達式,不區分大小寫 ( ~* )
  • 2. 匹配順序
    • 2.1 示例(精確匹配最高)
    • 2.2 示例(字串匹配次之)
    • 2.3 示例(正則匹間配高于通用匹配)
    • 2.4 示例(正則表達式間前邊的為準)
    • 2.5 示例(通用匹配兜底)
  • 3. 匹配間的沖突
    • 3.1 通用匹配 VS 字串匹配

1. 對url的匹配

1.1 默認匹配

  • 語法示例
    location /crow/ {
       return  501 "通用匹配\n";
    }

1.2 精確匹配( = )

  • 語法示例
    location = /crow/ {
       return  501 "精確匹配\n";
    }

1.3 正則,區分大小寫 ( ~ )

  • 語法示例
    location ~ /crow/.*\.md {
       return  501 "正則表達式,區分大小寫\n";
    }

1.4 正則表達式,不區分大小寫 ( ~* )

  • 語法示例
    location ~* /crow/.*\.md {
       return  501 "正則表達式,不區分大小寫\n";
    }

2. 匹配順序

  • 精確匹配(=
  • 字串匹配(^~
  • 正則匹配(~、~*
  • 默認匹配()

2.1 示例(精確匹配最高)

  • 配置文件內容:
server {
    listen    1840;
    root   /usr/share/nginx/html;
    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    location = /crow/test.md {
       return  501 "精確匹配\n";
    }
    location ~ /crow/.*\.md {
       return  501 "正則表達式,區分大小寫\n";
    }
    location ~* /crow/.*\.md {
       return  501 "正則表達式,不區分大小寫\n";
    }
    location ^~ /crow/test.md {
       return  501 "字串匹配\n";
    }
}
  • 訪問測試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
精確匹配

可見精確匹配被匹配到。

下邊我們去掉精確匹配:

2.2 示例(字串匹配次之)

  • 配置文件內容:
server {
    listen    1840;
    root   /usr/share/nginx/html;
    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    #location = /crow/test.md {
    #   return  501 "精確匹配\n";
    #}
    location ~ /crow/.*\.md {
       return  501 "正則表達式,區分大小寫\n";
    }
    location ~* /crow/.*\.md {
       return  501 "正則表達式,不區分大小寫\n";
    }
    location ^~ /crow/test.md {
       return  501 "字串匹配\n";
    }
}
  • 訪問測試

如下可見,還剩 字串匹配、正則匹配通用匹配,結果匹配到了 字串匹配。

[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
字串匹配

2.3 示例(正則匹間配高于通用匹配)

  • 配置文件
server {
    listen    1840;
    root   /usr/share/nginx/html;
    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    #location = /crow/test.md {
    #   return  501 "精確匹配\n";
    #}
    location ~ /crow/.*\.md {
       return  501 "正則表達式,區分大小寫\n";
    }
    location ~* /crow/.*\.md {
       return  501 "正則表達式,不區分大小寫\n";
    }
    #location ^~ /crow/test.md {
    #   return  501 "字串匹配\n";
    #}
}
  • 訪問測試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
正則表達式,區分大小寫

2.4 示例(正則表達式間前邊的為準)

上例中我們看到:~在前邊,因此先匹配了 ~。如果我們把~~*換個位置

  • 配置文件
server {
    listen    1840;
    root   /usr/share/nginx/html;
    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    location ~* /crow/.*\.md {
       return  501 "正則表達式,不區分大小寫\n";
    }
    location ~ /crow/.*\.md {
       return  501 "正則表達式,區分大小寫\n";
    }
}
  • 訪問測試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.md
正則表達式,不區分大小寫

2.5 示例(通用匹配兜底)

配置文件

我們還是將所有匹配都寫上

server {
    listen    1840;
    root   /usr/share/nginx/html;
    location / {
        index  index.html index.php index.htm;
    }
    location /crow/  {
       return  501 "通用匹配\n";
    }
    location = /crow/test.md {
       return  501 "精確匹配\n";
    }
    location ~ /crow/.*\.md {
       return  501 "正則表達式,區分大小寫\n";
    }
    location ~* /crow/.*\.md {
       return  501 "正則表達式,不區分大小寫\n";
    }
    location ^~ /crow/test.md {
       return  501 "字串匹配\n";
    }
}
  • 訪問測試
[root@liubei nginx-crow-test]# curl http://localhost:1840/crow/test.txt
通用匹配

3. 匹配間的沖突

3.1 通用匹配 VS 字串匹配

通用匹配字串匹配相同時,啟動報錯

  • 配置文件
    location /crow/test.md {
       return  501 "通用匹配\n";
    }
    location ^~ /crow/test.md {
       return  501 "字串匹配\n";
    }
  • 啟動報錯如下:
nginx-crow-test | nginx: [emerg] duplicate location "/crow/test.md" in /etc/nginx/conf.d/default.conf:45

以上就是nginx location指令(實戰示例匹配順序匹配沖突)使用詳解的詳細內容,更多關于nginx location指令的資料請關注其它相關文章!

分享到:
標簽:匹配 實戰 指令 示例 詳解
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定