03 nginx lcoation configuration

Time:2021-1-15

1. Location matching command

identifier describe
= Exact matchingBefore using it in standard URI, the request string and URI should be strictly matched. If the match is successful, stop the match and execute the request in the location immediately.
~ Regular matching; before regular URI, it means that the URI contains regular and case sensitive.
~* Regular matching; before regular URI, it means that the URI contains regular, case insensitive.
^~ Irregular matchingBefore using the standard URI, the nginx server will match the URI with the most prefixes, and regular matching will not be used after the pattern matching is successful.
nothing Normal matching (longest character matching)It has nothing to do with the location order, but gets the matching result according to the length of the matching. If it’s a perfect match, stop matching.

remarks

1. If the URI contains regular expressions, the ~ or ~ * identifier must be used;

2. For ~ and ~ * matching identifiers, you can add! Before them to negate them, as follows:

! ~ means regular mismatch, case sensitive.

! ~ * means regular mismatch, case insensitive.

2. Location matching

2.1 “=” accurate matching case

location  = /login {
#Exact match / login, after the match is successful, it ends immediately
}

2.2 “~” case sensitive regular matching cases

location ~ /images/ {
#Regular matching, case sensitive, end immediately after successful matching
}

2.3 “~ *” case insensitive regular matching cases

location ~* /images/ {
#Regular matching, case insensitive, ends immediately after successful matching
}

2.4 standard matching without regular matching

location ^~ /images/ {

#Match any address that starts with / images /. After matching, stop searching for regularization and use this one.

}

2.5 normal matching (longest character matching)

location  /blog/ {
#It has nothing to do with location order
#If the complete matching is successful, the matching will not continue, otherwise regular matching will be carried out
}

3 matching priority (matching order)

In the case of no identifier request, the matching rules are as follows:

1. The nginx server first searches multiple location blocks of the server block to see if there is a standard URI matching the request string. If there are multiple standard URIs that can be matched, the location with the highest matching degree is matched.

2. Then, nginx uses the location block to match the regular URI with the request string. If the regular matching is successful, the matching is ended, and the location is used to process the request; if the regular matching fails, the location with the highest matching degree in the standard URI is used.

remarks:

1. If there is an exact match, the exact match will be performed first. If the match is successful, the result will be returned immediately.

2. Ordinary matching has nothing to do with the order, because the matching results are obtained according to the length of the matching.

3. Regular matching is related to order, because it is a top-down matching. (match first, end parsing process)

4. In location, there is a unified location. All requests can be matched, as follows:

location  / {
#Since all addresses start with /, this rule will match all requests
#But regular and longest strings match first
}

Combined with the identifier, the matching order is as follows:

(location =) > (location full path) > (location ^ ~ path) > (location ~, ~ * regular order) > (location partial starting path) > (location /)
Namely:
(exact matching) > (longest string matching, but complete matching) > (irregular matching) > (regular matching) > (longest string matching, incomplete matching) > (location general matching)

practice

location = / {  
   //Exact match /, host name cannot be followed by any string
    Echo "rule a";
}
 
location = /login {
  //Exactly match the address at the beginning of / login. After matching, you will not continue to search 
    Echo "rule B";
}
 
location ^~ /blog/ { 
  //Non regular matching, after matching / blog /, stop searching for regular and use this one
  Echo "rule C";
}
 
 
location ~  \.(gif|jpg|png|js|css)$ {
    //Case sensitive regular matching, if the match is successful, stop searching for regular, use this one
    Echo "rule D";
}
 
 
location ~* \.png$ {  
   //Case sensitive regular matching, stop searching for regularities, use this one
    Echo "rule e";
}
 
location / {
  //Since all addresses start with /, this rule will match all requests
  //If there is no rule matching, use this rule
    Echo "rule F";
}
 
location /blog/detail { 
  //The longest string matching, if the complete matching is successful, it will not continue to match, otherwise it will be regular matching
  Echo "rule g";
}
 
location /images {  
    //Longest string matching, ditto 
    Echo "rule Y";
}
 
location ^~ /static/files {  
    //If the match is successful, it will not continue to match
    Echo "rule X";
}