转自:http://www.nginx.cn/115.htmlhtml
location匹配命令nginx
~ #波浪线表示执行一个正则匹配,区分大小写
~* #表示执行一个正则匹配,不区分大小写
^~ #^~表示普通字符匹配,若是该选项匹配,只匹配该选项,不匹配别的选项,通常用来匹配目录
= #进行普通字符精确匹配
@ #"@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files正则表达式
location 匹配的优先级(与location在配置文件中的顺序无关)
= 精确匹配会第一个被处理。若是发现精确匹配,nginx中止搜索其余匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说若是该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx中止搜索其余匹配,不然nginx会继续处理其余location指令。
最后匹配理带有"~"和"~*"的指令,若是找到相应的匹配,则nginx中止搜索其余匹配;当没有正则表达式或者没有正则表达式被匹配的状况下,那么匹配程度最高的逐字匹配指令会被使用。express
location 优先级官方文档ide
Directives with the = prefix that match the query exactly. If found, searching stops.fetch
All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.this
Regular expressions, in order of definition in the configuration file.spa
If #3 yielded a match, that result is used. Else the match from #2 is used.htm
=前缀的指令严格匹配这个查询。若是找到,中止搜索。rem
全部剩下的常规字符串,最长的匹配。若是这个匹配使用^〜前缀,搜索中止。
正则表达式,在配置文件中定义的顺序。
若是第3条规则产生匹配的话,结果被使用。不然,如同从第2条规则被使用。
例如
location = / { # 只匹配"/". [ configuration A ] } location / { # 匹配任何请求,由于全部请求都是以"/"开始 # 可是更长字符匹配或者正则表达式匹配会优先匹配 [ configuration B ] } location ^~ /p_w_picpaths/ { # 匹配任何以 /p_w_picpaths/ 开始的请求,并中止匹配 其它location [ configuration C ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg结尾的请求. # 可是全部 /p_w_picpaths/ 目录的请求将由 [Configuration C]处理. [ configuration D ] }
请求URI例子:
/ -> 符合configuration A
/documents/document.html -> 符合configuration B
/p_w_picpaths/1.gif -> 符合configuration C
/documents/1.jpg ->符合 configuration D
@location 例子
error_page 404 = @fetch;
location @fetch(proxy_pass http://fetch;)