nginx 匹配规则
location匹配顺序主要分为两块 第一块是对所有前缀字符串location做一次匹配,和对出现在nginx.conf中的顺序无关(通过2叉树放置了所有location字符串)。
场景1如果用户的请求和location中用=号指定的字符串完全匹配上,就停止做所有的匹配。满足条件
场景2 如果与一个前缀字符串上面标明了^~,即不用再进行下面的匹配了。
场景3如果多个匹配上,则使用最长匹配。即location后面跟的url最长的匹配上的。如果没有^~的,首先要记住最长匹配的前缀字符串location。因为后面如果正则表达式没有匹配上,仍然会使用最长匹配的前缀字符串location。做正则表达式的匹配是按nginx.conf中的顺序依次匹配正则表达式location。只要匹配上了 马上就会使用匹配上的正则表达式,不使用最长匹配的前缀字符串location。如果没有匹配上,则使用最长匹配的前缀字符串location。
开始测试
curl http://localhost/Test1/Test2
curl http://localhost/Test1/Test2/
curl http://localhost/Test1/Test2/3333
测试正则匹配 按照先后顺序匹配。将下面两条规则进行调换位置,测试同一个url访问的结果。
location ~* /Test1/(\w+)/$ {
return 200 'longest regular expressions match1!\n';
}
location ~ /Test1/(\w+)/$ {
return 200 'longest regular expressions match3!\n';
}