1. Location syntax rule

location [= | ^~ | ~ | ~* ] /uri/{ # ... } // Define nginx internal service jump location@name {}Copy the code

2. Location syntax

The rules Matching priority regular meaning
location = /uri 1 no = indicates an exact match. It takes effect only when the match is complete
location ^~ /uri 2 no Prefix matches the URL path beginning with ^~, and precedes the re
location ~ /uri 3 is The beginning indicates the beginning of a case-sensitive re match
location ~* /uri 4 is The beginning indicates case-insensitive regular matching
location /uri 5 no Without any modifiers, it also indicates a prefix match, but after the re match
location / 6 no Generic matching, any request that does not match another location will be matched, equivalent to the default in the switch

Note: The matching priority is from small to large, regardless of the location configuration order

3, location matching parsing

  • Test visit: http://localhost/home/index.html
  • Directory structure: /a/b/home/index.html
  • Agent nginx: http://localhost:8080
  • Proxy nginx HTML directory /index.html, /proxy_home/index.html

3.1. Root parsing

The parsing process

  1. Location and root are concatenated
  2. Concatenated ‘//’ is merged into ‘/’

Parsing example

location root Analytical results
/home html/a/b html/a/b/home/index.html
/home/ html/a/b html/a/b/home/index.html
/home html/a/b/ html/a/b/home/index.html
/home/ html/a/b/ html/a/b/home/index.html

3.2 Alias resolution

The parsing process

  1. Alias replaces the location-matched portion of the access address

Parsing example

location alias Analytical results
/home html/a/b /html/a/b/index.html
/home/ html/a/b /html/a/b/index.html
/home html/a/b/ /html/a/b//index.html
/home/ html/a/b/ /html/a/b/index.html

3.3 proxy proxy_pass resolution

The parsing process

  1. If there is no path after the proxy address port, the portion before the original access address location is replaced
  2. If there is a path after the proxy address port, then (as with alias resolution) replace the location matching portion and the portion before it

Parsing example

location proxy_pass Analytical results
/home http://localhost:8080 /home/index.html
/home/ http://localhost:8080 /home/index.html
/home http://localhost:8080/ //index.html
/home/ http://localhost:8080/ /index.html
/home http://localhost:8080/proxy_home /proxy_home/index.html
/home/ http://localhost:8080/proxy_home /proxy_homeindex.html
/home http://localhost:8080/proxy_home/ /proxy_home//index.html
/home/ http://localhost:8080/proxy_home/ /proxy_home/index.html

3.4. @name parsing

Defines nginx internal service jumps, which can be understood simply as a location handling function calls.

location / {
    root /wwwroot;
    try_files index.html index.htm @welcome;
}

# welcome
location @welcome{
    root /wwwroot/welcome;
    index index.html index.htm;
}
Copy the code