Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure location matching rules in nginx

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article to share with you is about how to configure location matching rules in nginx, Xiaobian think it is very practical, so share it with everyone to learn, I hope you can gain something after reading this article, not much to say, follow Xiaobian to see it.

location priority official documentation

Directives with the = prefix that match the query exactly. If found, searching stops.

All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.

Regular expressions, in order of definition in the configuration file.

If #3 yielded a match, that result is used. Else the match from #2 is used.

The = prefix directive matches this query exactly. If you find it, stop searching.

All the rest of the regular strings, the longest match. If this match uses the ^~ prefix, the search stops.

Regular expressions, in the order defined in the configuration file.

If rule 3 produces a match, the result is used. Otherwise, as applied from Rule 2.

for example

location = / { #matches only "/". [ configuration A ] } location / { #matches any request because all requests start with "/"#but longer character matches or regular expression matches will match [ configuration B ] } location ^~ /images/ { #matches any request starting with/images/and stops matching other location [ configuration C ] } location ~* \. (gif|jpeg)$ { #matches requests ending in gif, jpg, or jpeg.| jpeg)$ { # 匹配以 gif, jpg, or jpeg结尾的请求. The requested URL/images/banner.png was not found on this server. [ configuration D ] }

You can see that there are 5 different types of locations in the above example, of which the fourth with the prefix "~" is the location that needs regular matching. Nginx has different priority rules for these 5 different types of locations when parsing URLs. The general rules are as follows:

1, the string exactly matches a location with a "=" prefix, stop, and use the configuration of this location;

2. The string matches the rest of the irregular and non-special locations. If it matches a location with a prefix of "^~", it stops.

3, regular matching, matching order is the order in which location appears in the configuration file. If it matches a regular location, stop and use the location configuration; otherwise, use the location configuration with the largest string match obtained in step 2.

For example, for the following requests:

1,/ -> exactly match to the first location, match stops, use configuration A

2,/some/other/url -> First prefix part of the string matches to the second location, Then regular matching, Obviously no match, Then use the second location configuration B

3,/images/1.jpg-> First the prefix part of the string matches the 2nd location, but then the prefix matches the 3rd location, and at this time it is already the largest string match for this url in the configuration file, and the location has a prefix of "^~", then no longer regular matching, finally use configuration C

4,/some/other/path/to/1.jpg -> First, the prefix part of the same string matches to the second location, and then regular matching, then regular matching is successful, then use congiuration D

Request URI example:

/ -> Compliance with configuration A/documents/document.html -> Compliance with configuration B/images/1.gif -> Compliance with configuration C/documents/1.jpg -> Compliance with configuration D@location Example error_page 404 = @fetch;location @fetch(proxy_pass http://fetch;)

location matching command

~ #wavy line means to perform a regular match, case-sensitive

~* #means to perform a regular match, case insensitive

^~ #^~ indicates a common character match. If the option matches, it only matches that option and does not match other options. It is generally used to match directories.

= #Exact matching of ordinary characters

@ #"@" defines a named location, when used internally, for example error_page, try_files

Priority of location matches (regardless of the order of locations in the configuration file)

= Exact matches will be processed first. If an exact match is found, nginx stops searching for other matches.

Normal character matches, regular expression rules and long block rules will be given priority for matching queries, which means that if the term matches, it is necessary to see if there are regular expression matches and longer matches.

^~ Then only that rule is matched, nginx stops searching for other matches, otherwise nginx continues to process other location directives.

The last match deals with directives with "~" and "~*". If a match is found, nginx stops searching for other matches; when no regular expression is found or no regular expression is matched, the highest matching word-by-word directive is used.

The above is how to configure location matching rules in nginx, Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report