标签:
Location匹配的url的语法规则:
location [=|~|~*|^~] /uri/ { … }
= 表示精确匹配
~ 表示区分大小写的正则匹配
^~ 表示以某个常规字符串开头的url即可;
~* 表示不区分大消息的正则匹配
!~(*!~)表示不区分大小写不匹配的正则
/ 通配符,任何请求都能匹配到这个location,如果有任何其他location可以匹配,则会覆盖该location
匹配顺序:
1)先匹配普通url,在匹配正则
2)“普通 location ”的匹配规则是“最大前缀”,因此“普通 location ”的确与 location 编辑顺序无关;
3)location的执行逻辑基本上跟顺序没有关系;但是针对正则匹配的方式,匹配上第一个url,就不在继续匹配后面的url;
这种情况,如果匹配上普通localtion,没有正则匹配,则使用普通匹配;如果既有普通location的最大前缀匹配,也有正则匹配,则正则匹配覆盖最大前缀匹配。
4)匹配完“普通 location ”后,有的时候需要继续匹配“正则 location ”,有的时候则不需要继续匹配“正则 location ”。
两种情况下,不需要继续匹配正则 location :(1) 当普通 location 前面指定了“ ^~ ”,特别告诉 Nginx 本条普通 location 一旦匹配上,则不需要继续正则匹配;(2) 当普通location 恰好严格匹配上,不是最大前缀匹配,则不再继续匹配正则。
例如:
location = / {
# matches the query / only.
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
[ configuration B ]
}
location /documents/ {
# matches any query beginning with /documents/ and continues searching,
# so regular expressions will be checked. This will be matched only if
# regular expressions don‘t find a match.
[ configuration C ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration D.
[ configuration E ]
}
请求示例,匹配的Location:
标签:
原文地址:http://www.cnblogs.com/yyyt/p/4322094.html