16. nginx的location模块
location的匹配规则¶
- 总原则:先前缀字符串,再正则表达式。
1 前缀字符串
a、location = /data/ {} 精确匹配 b 、location ^~ /data/ {} 匹配上后不在进行正则表达式匹配 c、location /data/ {} 常规匹配
d、location ~ \.html? {} 大小写敏感的正则匹配 e、location ~* \.html? {} 忽略大小写的正则匹配
3 匹配原则
实战验证¶
nginx配置
server{ server_name location.abc.com; location ~ /Test1/$ { return 200 "first regular expression match!"; } location ~* /Test1/(\w+)$ { return 200 "longst regular expression match!"; } location ^~ /Test1/ { return 200 "stop regular expression match!"; } location /Test1/Test2 { return 200 "longst prefix expression match!"; } location /Test1{ return 200 "prefix expression match!"; } location = /Test1{ return 200 "exac match!"; } }
[root@localhost vhosts]# curl location.abc.com/Test1 exac match! [root@localhost vhosts]# curl location.abc.com/Test1/ stop regular expression match! [root@localhost vhosts]# curl location.abc.com/Test1/Test2 longst regular expression match! [root@localhost vhosts]# curl location.abc.com/Test1/Test2/ longst prefix expression match! [root@localhost vhosts]# curl location.abc.com/Test1/Test2/sdsdda longst prefix expression match! 最后一个是最长匹配,一旦没有能够匹配到的东西,那就找最长匹配