location几种匹配模式
location = /uri 精确匹配,不能使用正则且区分字符大小写
location ^~ /uri 前缀匹配,不能使用正则且区分字符大小写
location ~ 正则匹配,区分字符大小写
location ~* 正则匹配,不区分字符大小写
location /uri 正常匹配(正常匹配和前缀匹配的差别在于优先级。前缀匹配的优先级高于正常匹配),不能使用正则且区分字符大小写
location / 全匹配
匹配模式优先级:精确匹配 > 前缀匹配 > 正则匹配 > 正常匹配 > 全匹配
精确匹配
location = /demo {
rewrite ^ https://blog.whsir.com;
}
只有访问http://10.10.10.10/demo这样的地址才能匹配,哪怕是这种地址(http://10.10.10.10/demo/)都是不可以的,并且精确匹配不能使用正则。
前缀匹配
location ^~ /demo {
rewrite ^ https://blog.whsir.com;
对于该模式/demo,以下地址都能匹配
http://10.10.10.10/demo
http://10.10.10.10/demo/
http://10.10.10.10/demo/123
http://10.10.10.10/demo/123a/bbb
http://10.10.10.10/demo/AAA
http://10.10.10.10/demo/Ab/cd
http://10.10.10.10/demo123
http://10.10.10.10/demo.aaa
只要以/demo为前缀开头的url都能匹配。
正则匹配区分大小写
location ~ /[0-9]emo {
rewrite ^ https://blog.whsir.com;
}
对于该模式/[0-9]emo,以下地址都能匹配
http://10.10.10.10/2emo
http://10.10.10.10/3emo
http://10.10.10.10/4emo/aaa
http://10.10.10.10/5emo/AAA
http://10.10.10.10/6emoaaa
只要以正则表达式/[0-9]emo匹配的字符开头的url,都能匹配。
正则匹配不区分大小写
location ~* /[0-9]emo {
rewrite ^ https://blog.whsir.com;
}
对于该模式/[0-9]emo,以下地址都能匹配,此时是不区分大小写的
http://10.10.10.10/2Emo
http://10.10.10.10/3eMo
http://10.10.10.10/4emO/aaa
http://10.10.10.10/5EMo/AAA
http://10.10.10.10/6eMOaaa
正常匹配
location /demo {
rewrite ^ https://blog.whsir.com;
}
对URI的左半部分做匹配检查,不能使用正则且区分字符大小写
http://10.10.10.10/demo
http://10.10.10.10/demo/
http://10.10.10.10/demo/123
http://10.10.10.10/demo/123a/bbb
http://10.10.10.10/demo/AAA
http://10.10.10.10/demo/Ab/cd
http://10.10.10.10/demo123
http://10.10.10.10/demo.aaa
全匹配
location / {
rewrite ^ https://blog.whsir.com;
}
匹配任何请求,优先级最低
注:本文转自https://blog.whsir.com/post-4101.html
Be First to Comment