742   Nginx

一,首先翻译下官网文档中关于location的说明

Syntax: location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
Default: —
Context: server, location

语法:location [ = | ~ | ~* | ^~ ] uri { … }

A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching).

location 匹配可以是前缀字符串,或者是正则表达式。正则表达式有两种:”~*”,不区分大小匹配;”~”,区分大小匹配

To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered.

首先匹配的是前缀字符串的location。在所有前缀字符串中,匹配成功的最长的前缀字符串的location将会被选中并且被记住。

Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.

接着匹配正则表达式的location,是按照它们在配置文件的顺序来执行匹配的。只要一遇到匹配成功的正则,后面的正则表达式的location将不会去匹配了,直接执行返回;如果所有正则表达式的loaction都没有匹配成功,那么之前被记住的location的匹配结果将会被选用。

If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.

如果前缀字符串匹配中的location,最长前缀字符串使用了”^~”,那么正则表达式的location的匹配将不会执行。

Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.

如果使用了”=”进行精确匹配,而且匹配成功,那么所有后面的location匹配将会停止。这个有助于响应提升速率。

 

 

二,总结一下location语法
1,精确匹配:”=”
匹配网站首页:http://www.example.com/

location = / {
   
}

匹配具体路径:http://www.example.com/about.html

location = /about.html {
    
}

2,前缀字符串匹配:匹配成功的最长前缀字符串的location将会被选中并记住,接着会匹配正则表达式location
匹配/user/开头的路径:http://www.example.com/user/show/1

location /user/ {
    
}

3,前缀字符串匹配:使用”^~”,如果匹配成功,而且是最长前缀字符串,将会停止匹配正则表达式的location

匹配/admin/setting开头的路径:http://www.example.com/admin/setting

location /admin/ {
    
}
location ^~ /admin/setting {
    [ configuration A ]
}

4,正则匹配:”~*”,不区分大小的正则匹配

匹配路径结尾是”.gif”,”.jpg”,”.png”,而且不区分大小:
http://www.example.com/static/123.jpg
http://www.example.com/static/123.PNG

location ~* .(gif|jpg|png)$ {
    
}

5,正则匹配:”~”,区分大小的正则匹配

匹配路径开头是”/userAction”,而且区分大小:
http://www.example.com/userAction/setting

location ~ ^/userAction/ {
    
}

 

三,总结location执行顺序:

1,首先进行精确匹配”=”,如果匹配成功,将会停止后面所有location的匹配,并执行响应;否则执行第2步前缀字符串匹配;
2,接着匹配前缀字符串,在所有前缀字符串中,匹配成功的最长的前缀字符串的location将会被选中并且被记住;如果最长的前缀字符串使用”^~”,将会停止匹配后面正则表达式的location,并执行响应;否则,记住匹配成功的最长前缀字符串的location的匹配结果,然后执行第3步正则匹配;
3,最后执行正则匹配”~*”和”~”,只要一遇到匹配成功的location,将会停止匹配后面正则表达式的location,并执行响应;如果所有正则都匹配不成功,那么执行第2步记住的匹配结果

 

如下顺序:
1,=
2,^~ 最长字符串
3,~*
4,~
5,最长字符串

 

举例说明

# location A
location = / {
rewrite ^(.*)$ http://www.baidu.com/?location=A;
}
# location B
location / {
    rewrite ^(.*)$ http://www.baidu.com/?location=B;
}
# location C
location /user/ {
    rewrite ^(.*)$ http://www.baidu.com/?location=C;
}
# location D
location /user/setting {
    rewrite ^(.*)$ http://www.baidu.com/?location=D;
}

# location E
location ^~ /images/ {
    rewrite ^(.*)$ http://www.baidu.com/?location=E;
}
# location F
location ^~ /images/upload {
    rewrite ^(.*)$ http://www.baidu.com/?location=F;
}
# location G
location ~* \.(gif|jpg|png)$ {
    rewrite ^(.*)$ http://www.baidu.com/?location=G;
}

 

1,http://www.example.com/,匹配到location A ;

采用精准匹配”=”,匹配成功后,不再匹配后面的location

 

2,http://www.example.com/index.html,匹配到location B ;

 

3,http://www.example.com/user/,匹配到location C ;

 

4,http://www.example.com/user/setting,匹配到location D ;
虽然也符合location C,但是匹配成功的最长前缀字符串的location将会优先选中;

 

5,http://www.example.com/user/header.jpg,匹配到location G ;

因为没有采用符号”^~”,所以后面的正则表达式的location将会继续匹配;

 

6,http://www.example.com/user/setting/header.jpg,匹配到location G ;
因为没有采用符号”^~”,所以后面的正则表达式的location将会继续匹配;

 

7,http://www.example.com/images/,匹配到location E ;

 

8,http://www.example.com/images/upload,匹配到location F ;

虽然也符合location E,但是匹配成功的最长前缀字符串的location将会优先选中;

 

9,http://www.example.com/images/header.png,匹配到location E ;

因为采用符号”^~”,所以后面的正则表达式的location将不会执行匹配;

 

10,http://www.example.com/images/upload/header.png,匹配到location F ;

因为采用符号”^~”,所以后面的正则表达式的location将不会执行匹配;

 




Leave a Reply

Your email address will not be published. Required fields are marked *