In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces what is the Location matching priority of Nginx. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
The Location in Nginx's configuration file is used to match a specific URI. If the rule following the location matches the URI, and the match stops matching backward here, the rule under the location is applied and the match continues until the corresponding rule is matched or the default rule is matched.
Here we only discuss the positioning of location, and the other parts are not parsed.
Problem solved:
Why is location important?
Location is equivalent to selecting, selecting. When we deal with something, we often have to choose it and then take further action. For example, when dealing with word, when you apply a font to a paragraph of text, you must select it first and then apply the rules. Location means that the selected object is URI, according to different URI, using different rules, location is the first step, only this step is correct, the rest can be guaranteed to proceed smoothly.
How should location be configured?
Several matching methods of location
Normal match:
Location = URI {configuration} # exact match
Location ^ ~ URI {configuration} # irregular matching
Location [space] URI {configuration} # # prefix match
Regular matching
Location ~ URI {configuration} # case sensitive matching
Location ~ * URI {configuration} # case insensitive match
Priority of several matching methods
The overall rules follow the normal match first, then the regular match, and if the regular match does not match, fall back to the previous normal match. Among them, there is no order for ordinary matching, which matching is the most accurate, which location is used, and regular matching is carried out according to the writing order of the rules.
= exact matching, stop subsequent matching after matching, and directly execute the configuration after matching.
[space] prefix matching, after matching, continue longer prefix matching and regular matching.
^ ~ non-regular matching, after matching the rule, stop continuing regular matching.
~ case-sensitive regular matching, matching in order, and stopping subsequent matching once the match is on.
~ * case-insensitive matching. Once the match is made, the subsequent matching is stopped.
Similarities and differences of several matching rules
First, number several matching symbols:
Symbol
Serial number
=
one
Space
two
^ ~
three
~
four
~ *
five
1 vs 2
Similarity: the special case of 2 (implicit exact matching) will be equal to 1
Differences: 1 stop the subsequent regular matching after matching, 2 see if there is a longer prefix to match, and whether there is any subsequent regular matching.
1 vs 3
The same point: all stop subsequent regular matching, that is, even if there is a regular expression to match, it will not match.
The difference: 3 the maximum prefix matching continues after the match, and if a more precise match is available, use this match.
1 and 45 are two different types of matches, and matching 1 means that 45 is ignored.
2 vs 3:
Similarities: maximum prefix matching is performed after matching.
Differences: 2 regular matching is carried out after maximum prefix matching, and regular matching is stopped after 3 matching.
Example:
If the URI is the same, then location [] / test and location ^ ~ / test are the same, with the following error:
Nginx: [emerg] duplicate location "/ test" in / data/nginx/conf/nginx.conf:42
Thus it can be seen that ^ ~ and spaces have the same meaning in the case of URI, so the error is reported as above.
Demo:
In the following path
/ data/nginx/zzp/exact/
/ data/nginx/zzp/regex/
Create a file with the same name but different content, and then use curl-v http://host/a.html
It is found that the regular expression part will take effect, which proves that after the URI after the ordinary matching, the regex matching continues, and after the regex matching, the regex takes effect.
Location / zzp/ {
Alias / data/nginx/zzp/exact/
}
Location ~ ^ / zzp/ (. *\ .html) ${
Alias / data/nginx/zzp/regex/$1
}
Using the cancel regular expression symbol ^ ~, and then request the same link, it is found that the file under the exact path is accessed, which proves that regular expression matching is no longer performed after ordinary matching is performed.
Location ^ ~ / zzp/ {
Alias / data/nginx/zzp/exact/
}
Location ~ ^ / zzp/ (. *\ .html) ${
Alias / data/nginx/zzp/regex/$1
}
So much for sharing what the Location matching priority of Nginx is. I hope the above content can be helpful to you and you can learn more. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.