nginx configuration location method summary

  • 2020-05-15 03:40:09
  • OfStack

location matching order

1. The "=" prefix instruction matches, and if the match is successful, the other matches will be stopped
2. Normal string instruction matching, the order is from long to short. If location successfully matches with ^~, other matching (regular matching) will be stopped.
3. Regular expression instruction matching, according to the order in the configuration file, successful stop other matching
4. If there is a successful match in step 3, the result will be used; otherwise, the result in step 2 will be used

Pay attention to the point

1. The order of matching is to match the normal string first, then the regular expression. In addition, the normal string matching order is from long to short according to the length of the characters in the configuration. That is to say, the order of location configured with the normal string is irrelevant. In the end, nginx will match according to the length of the configuration, but it is important to note that the regular expression is tested according to the order in the configuration file. Finding the first matching regular expression will stop the search.

2.1 normally, the regular expression location will be matched after the normal string location is successfully matched. There are two ways to change this behavior. One is to use the "=" prefix, where a strict match is performed and the other matches are stopped as soon as the match is successful, while the request is processed. The other is to use the "^~" prefix, which if applied to a regular string tells nginx not to test the regular expression if the path matches.

Match patterns and sequences

The beginning of location = /uri = indicates an exact match, which only takes effect if it is a perfect match.

location ^~ /uri ^~ prefixes the URL path before the regex.

The beginning of location ~ pattern ~ represents a case-sensitive regular match.

location ~* pattern ~* begins with a regular matching that is case-insensitive.

location /uri does not have any modifiers and also means a prefix match, but after a regular match.

location/general match, any request that is not matched to another location will be matched, equivalent to default in switch.

The experiment case

To test "^~" and "~", nginx is configured as follows. Browser input http: / / localhost helloworld/test, back to 601. As annotation will be # 1, # 2 opened, the browser input http: / / localhost helloworld/test, back to 603. Note: #1 and #2 cannot be opened at the same time. If they are opened at the same time, launch nginx to report nginx: [emerg] duplicate location "/helloworld"... Because both of these are normal strings.


location ^~ /helloworld {  #1
 return 601;
}
  
#location /helloworld {  #2
# return 602;
#}

location ~ /helloworld {
 return 603;
} 

Test the length of a normal string (the matching of a normal string is not a matter of order, but of length). Browser input http: / / localhost helloworld test/a html, back to 601. Browser input http: / / localhost helloworld/a html, back to 602.


location /helloworld/test/ {  #1
 return 601;
}
  
location /helloworld/ {    #2
 return 602;
}

Test the order of the regular expression (regular matching is related to the order). Browser input http: / / localhost helloworld test/a html, return 602; Will exchange order # 2 and # 3, and browser input http: / / localhost helloworld test/a html, back to 603


location /helloworld/test/ {  #1
 return 601;
}

location ~ /helloworld {   #2
 return 602;
}
  
location ~ /helloworld/test {  #3
 return 603;
}

Therefore, in practice, I think there are at least 3 match rule definitions, as follows:


# Directly matching the website root, through the domain name to visit the website home page more frequently, using this will speed up the processing, the official website said.  
# This is directly forwarded to the back-end application server, or it could be 1 Static home page  
#  The first 1 Three mandatory rules  
location = / { 
 proxy_pass http://tomcat:8080/index 
} 
 
#  The first 2 The required rule is to handle static file requests, which is nginx As a http Server strengths  
#  There are two configuration patterns, directory matching or suffix matching , Choose the 1 Or in combination  
location ^~ /static/ { 
 root /webroot/static/; 
} 
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { 
 root /webroot/res/; 
} 
 
# The first 3 These rules are generic rules for forwarding dynamic requests to back-end application servers  
# Non-static file request is the default dynamic request, according to the actual grasp  
# After all, the current 1 Some frames are popular with tape .php,.jsp Suffixes are rare  
location / { 
 proxy_pass http://tomcat:8080/ 
}

Related articles: