nginx Configuration Note - location module

Nginx location matching rules

- performing a wavy line represents a regular match, a case-sensitive 
~ * represents a regular perform matching insensitive 
^ ~ ^ ~ represents an ordinary character matches, if the option is matched, the match only option, other options do not match, the general directory to match 
= exact match for normal character 
when # @ "@" defines a LOCATION named, used internally directed, e.g. error_page, try_files

 

location matching priority

= Exact match will be processed first. If you find an exact match, nginx stop searching for other matches. 
Ordinary characters match the regular expression rules and long blocks will be matched first, that if the match needed to see there is no regular expression matching and longer match. 
^ ~ Only match this rule, nginx stops searching other matches, otherwise nginx will continue to handle other location instructions. 
Li last match with the "~" and "~ *" command, if the corresponding match is found, the search stops nginx other match; when the absence of a regular expression or a regular expression is not matched, then the highest degree of matching matched literally instruction will be used.

 

Examples

= LOCATION / { 

# matches only " / " . 

[Configuration A] 
} 
} 
L 
LOCATION / { 

# matches any request, because all requests are based on " / " Start 

# character match or longer but regular expression match priority match 

[Configuration B] 
} 
} 
L 
LOCATION ^ ~ / images / { 

# matches any / images / start request and stops matching other LOCATION 

[Configuration C] 
} 
} 
L 
LOCATION ~ * \. (GIF | JPG | JPEG) $ { 

# matches gif, jpg, or jpeg end request. 

# but all / images /Requested by the directory [Configuration C] process. 

[Configuration D] 
} 
}

 

try_files usefulness

location  / {
   root  /var/www/build;
   index  index.html index.htm;
   try_files $uri $uri/ @rewrites;
}
  
location @rewrites {
   rewrite ^(.+)$ /index.html last;
}

 

try_files literally is trying to file, combined with understanding of the environment is "trying to read the file" (read static files)
 
This is a $ uri variable nginx, and store the address of the user's access,
For example: http: //www.xxx.com/index.html, then $ uri is /index.html
 
$ Uri / Representative's visit is a directory, such as: http: //www.xxx.com/hello/test/, then $ uri / is / hello / test /
 
Complete explanation is: try_files to try to access the web directory to read the user's file, if the first variable exists, direct return;
Absence continues to read the second variable, if present, is returned directly; no direct jump to the third parameter.
 
 
For example, users access the web address: http: //www.xxx.com/test.html
try_files will first judge he is a file or a directory, and found that he is a file, the first parameter $ uri variable match.
Then go to the website directory to go to find out whether test.html file exists, if there is a direct reading return. If there is no direct link to the third argument, the third argument is a location, and this location is inside the configuration rewrite rule.
 

index usefulness

index index.php index.html index.htm i.html;
 try_files $ on $ a / /index.html;

# Index define the default directory name lookup files such as Pham Van http://www.xxx.com/love will first look for love no longer find the file directory if there is love in turn directory to find love
# Index.php index.html index.htm i.html file returns
 
  
 
Examples
    {Server 
        the listen 80; 
        server_name localhost; 
        the root / the root / SCC / dist; 

        . # The default for the Load Configuration Server Block Files 
        the include /etc/nginx/default.d/*.conf; 

        # static file, nginx own processing 
        location ~ * . \ (JS | CSS | Flash | Media | JPG | PNG | GIF | DLL | CAB | CAB | ico | HTML | HTM | json | TXT | MP3) $ { 
          the Expires 30d; 
        } 

        LOCATION / { 
          # index defined in the default directory lookup name of the file 
          index index.php index.html index.htm i.html; 
          # matches any request, because all requests are based on a "/" single-page application unified point index.html  
          # but more character or regular expression match priority will match such as match / api / forwarded to the back-end interface to a static resource
          # when a user requests http: // localhost / example when, where $ uri is / example.
          # Try_files to the hard disk will try to find this file. If there are named / $ root / example (which is $ root project code installation directory) file, send it directly to the contents of this file to the user. 
          # Clearly, there is no directory called the example files. And then look at $ uri /, added a /, that is, to see if the name / $ root / example / directory. If there is a default lookup index file in the directory or returned 403 
          # If they can not find a directory, it will fall back to try_files last option /index.html, launched an internal "sub-request", which is equivalent to nginx initiate an HTTP request to http: //localhost/index.html. 
          In this case $ # /index.html URI is found in the root directory of the file items successfully matched 
          try_files $ $ URI URI / /index.html; 
        } 

        error_page 404 /404.html; 
        LOCATION /40x.html = { 
        } 

        error_page 500 502 /50x.html 504 503; 
        LOCATION /50x.html = {  
        }
    }

  



 

Guess you like

Origin www.cnblogs.com/shichangchun/p/10990375.html