[nginx] Detailed explanation of location rules of nginx:


1. Grammatical rules:

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配url路径即可(非正则)

~ 开头表示区分大小写的正则匹配

~* 开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则

/ 通用匹配,任何请求都会匹配到

2. Priority:

等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他location的匹配项

^~和通用匹配。使用前缀匹配,不支持正则表达式,如果有多个location匹配成功的话,不会终止匹配过程,会匹配表达式最长的那个(下方有例子)

如果上一步得到的最长的location为^~类型,则表示阻断正则表达式,不再匹配正则表达式

如果上一步得到的最长的location不是^~类型,继续匹配正则表达式,只要有一个正则成功,则使用这个正则的location,立即返回结果,并结束解析过程

insert image description here

3. Verification:

1. Exact match:

Create a configuration file test.com.conf under the conf.d folder, the content is as follows:
insert image description here
the first and second location matching conditions in the above figure are the same as /test.html, but the second is an exact match to static Path, so the first one will not be executed, and the second one will be executed. www.test.com is the local domain name resolution, and access_log and error_lor can define logs for each module separately. The content after
accessing through the domain name and path is as follows:
insert image description here
注意:路径/usr/share/nginx/test_html文件夹下需要有test.html才可以正常访问

The keyword root used to specify the static resource path in the above figure can also use alias, so what is the difference between root and alias?
(1) The value specified with the root attribute is to be added to the final path, and the matching conditions will be spliced ​​into the path
(2) The value specified with the alias attribute does not need to be added to the final path

As shown in the figure above, the request condition is test.html, and the path specified by root is /usr/share/nginx/test_html, so when matching, the file test.html must exist under this path. Otherwise, it will not be found and an error will be reported. If alias is used, when the request is made through the browser, the alias is also specified in the /usr/share/nginx/test_htm path, but it will match the default index.html without forcing the match test.html, but you cannot use "=" for exact matching at this time, now change root to alias, as shown in the figure:
insert image description here
注意:alias指定的路径结尾要加”/”

The following configuration file uses the path specified by roo. When http://www.test.com/html/ is requested through the domain name, it will jump to the index.html page under /usr/share/nginx/html/, as shown in the figure :
insert image description here

The following configuration file uses alias to specify the path. When requesting http://www.test.com/linshi/, it will jump to the /usr/share/nginx/test_html/index.html page, as shown in the figure:
insert image description here

2. Use ^~ to achieve matching:

For example: the following configuration file has two rules, which match URLs starting with the letter a, but the lengths are different. First, comment out the long rules first, as shown in the figure:
insert image description here

View the status code through the curl request, as shown in the figure:
insert image description here

说明:Currently only one rule is enabled, so when matching any url starting with /a/, status code 666 will be returned

Now uncomment the second rule, open the rule, as shown in the figure:
insert image description here
initiate the same request again, and observe the returned status code, as shown in the figure:
insert image description here

As can be seen from the figure above, the two rules are matched successfully at the same time, but the second rule is relatively long, so the second rule is matched first. If ^~ matches successfully, it means that the regular expression is blocked and no more regular match

3. Matching is achieved through the "~" method:

The matching rules in the above figure are all implemented through the "^~" method, so there are two cases when matching the longest rule: the first one
: the longest rule is matched through ~ (in the above figure The longest rule is achieved through ~)
The second type: the longest rule is not achieved through matching through ^~, but through ordinary matching

When the longest rule passes normal matching, regular matching will continue. As long as one regular rule is successful, the location of this regular rule will be used to stop matching and return the result. Now change the second rule to normal matching and add
a Regular matching, as shown in the figure:
insert image description here
Execute the same request command again, and the obtained status code is as follows:
insert image description here

说明:It can be seen from the figure above that when requesting /a/b/, the second rule (the longest rule) will be reached first. Since the second rule is a normal match (not a ^~ match), it will continue to match the regular pattern. That is the third rule, so the final return status code is 888

4. Matching is achieved by "~*":

"~*" indicates a case-insensitive regular match.
For example: request /a/b/ or /A/B/ through the url, and check the return status code, as shown in the figure: The
insert image description here
request result is as follows: As
insert image description here
can be seen from the above figure, no matter the match Whether it is uppercase or lowercase, it will return the corresponding status code 888.
The common method also matches the image suffix, and returns the specified information, as shown in the figure:
insert image description here
insert image description here

5. "!~*" and "!~" are not commonly used, so I won't introduce them again
6. Use "/" to achieve universal matching:

In this example, the 9091 service is
the first case of httpd: there is no slash at the end of proxy_pass, and there is a slash (/bbb/) in the matching path, as shown in the figure:
insert image description here
说明:there is no slash "/" at the end of proxy_pass, and the browser requests at this time http://10.9.2.248/bbb/, then the actual access address is http://10.9.2.248:9091/bbb/, and the matching path /bbb will be added together.
At this time, if it is in the http directory page directory htdocs Create a directory bbb, create a file index.html under the bbb directory, and then you can achieve normal access, as shown in the figure:
insert image description here

The second case: there is a slash "/" at the end of proxy_pass, and the matching path also has a slash (/bbb/), as shown in the figure: Explanation: There is a slash
insert image description here
"/" at the end of proxy_pass, and the browser requests http:/ /10.9.2.248/bbb/, then the actual access address is http://10.9.2.248:9091, and /bbb will be discarded, as shown in the figure:
insert image description here

The third case: there are other paths behind proxy_pass but there is no "/" at the end, and the matching path also has a slash (/bbb/), as shown in the figure: Explanation, at this time,
insert image description here
visit http://10.9.2.248/bbb/ through a browser index.html, the actual request is http://10.9.2.248/cxxindex.html (note that the location is under the default path, not under the ccc path, if the path of proxy_pass is /ccc/ddd, then the actual request is under the ccc path cccindex.html)
Create the file cxxindex.html under the default path of httpd (htdocs), and then access it, as shown in the figure:
insert image description here

The fourth case: there are other paths behind proxy_pass but there is "/" at the end, and the matching path also has a slash (/bbb/), as shown in the figure: Description: At this time,
insert image description here
visit through a browser: http://10.9.2.248/bbb /index.html, the actual visit is http://10.9.2.248/ccc/index.html
Create a folder named ccc under the default path of httpd (htdocs), define an index.html inside, and then visit, As shown in the picture:
insert image description here

The fifth case: there is no "/" at the end of the location matching path, and there is no "/" after proxy_pass, as shown in the figure: Note:
insert image description here
Port 8081 is the server port of httpd. If there is no "/" after the matching path and proxy_pass, then at this time Visit http://10.9.2.248/bbb, and the content of http://127.0.0.1:8081/bbb/index.html will be requested by default. At this time, the folder bbb will be created under the default path htdocs of httpd, and the index will be created inside. html, the definition content is: i am match bbb, the request through IP is as shown in the figure:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_53791978/article/details/131927724