In Nginx we can configure the location directive block to determine how a request URL is handled. If we write multiple location blocks, how can we ensure that each location does not conflict? How to clarify the matching order of location?
With these two questions in mind, let’s do some practice problems first.
Original address, welcome to Star and subscribe to my blog.
Nginx Location exercises
Question 1: Which config will be matched if /abcd is accessed?
server {
location ~ /abc {
#config 1
}
location /abc {
#config 2
}
location^ ~ /abc {
#config 3}}Copy the code
Question 2. Which config will be matched if /abcd is accessed?
server {
location ~ /abc {
#config 1
}
location /abc {
#config 2
}
location^ ~ /ab {
#config 3}}Copy the code
Question 3. Which config will be matched if /abcd is accessed?
server {
location /abc {
#config 2
}
location^ ~ /ab {
#config 3}}Copy the code
If these three exercises are confusing to you, we’ll dig deeper and you’ll find something. If you just want to know the answers, please go to the end of this article.
Nginx Location configuration syntax
Nginx location configuration syntax is as follows
The location [= | | | ~ ~ * ^ ~ | empty] url {... }Copy the code
Among them [= | | | ~ ~ * ^ ~ | empty] said the location of the modifier, specific meaning is as follows
- = indicates an exact match (a rule is hit only when it is a perfect match).
- ~ indicates case-sensitive regular matching.
- ~* indicates case-insensitive regular matching.
- ^~ indicates the best match.
- Empty, matches a string that starts with a URL, which can only be a normal string.
Nginx location matching process
In general, an nginx.conf file is usually configured with multiple matching rules. If multiple matching rules match the URL, which one will be executed?
1, Nginx first checks the longest prefix string against the URL, that is, determines the contents of the [=], [^~], [null] modifier.
- If matches to the longest match prefix string.
- If the longest matching prefix string is matched by the [=] modifier, the response is immediate.
- If it is not matched by the [=] modifier, step 2 is performed.
- If no longest matching prefix string is matched, step 3 is performed.
2. Nginx continues to check the longest matching prefix string, that is, to determine the contents of the [^~], [null] modifier definition.
- If the longest matching prefix string is matched by the [^~] modifier, the response is immediate.
- If it is matched by the null modifier, the match is saved and the step 3 judgment is performed.
3. Nginx finds all regular matches (~ and ~*) defined in nginx.conf and matches them in order.
- If any regular expression matches successfully, the response is immediate.
- If no re matches are successful, the empty match stored in step 2 is responded to.
Answer:
Problem 1: The longest matching string is ABC, and there is a ^~ match, so the result is config3.
Problem 2: The longest matching string is ABC, which is matched by [null]. Therefore, the re match is searched, and the result is config1 because ~ / ABC is matched.
Problem 3: The longest matching string is ABC, which is matched by [null]. Therefore, the system starts to search for re matches. No re is matched, so the result is config2.
The resources
Understanding Nginx Server and Location Block Selection Algorithms