Detail the Nginx location matching rules

  • 2020-05-13 04:33:07
  • OfStack

Grammar rules

location [=|~|~*|^~] /uri/ {... }

模式 含义
location = /uri = 表示精确匹配,只有完全匹配上才能生效
location ^~ /uri ^~ 开头对URL路径进行前缀匹配,并且在正则之前。
location ~ pattern 开头表示区分大小写的正则匹配
location ~* pattern 开头表示不区分大小写的正则匹配
location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
location / 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default

When the prefix is matched, Nginx does not encode url, so the request is /static/20%/aa, which can be matched by the rule ^~ /static/ /aa (note space).

In the case of multiple location configurations, the matching order is as follows (for reference, it has not been actually verified, just try it, just for reference) :

First of all, the exact match = The second prefix matches ^~ The second is regular matching in the order of the file Then match the prefix match without any modification. Finally, give/universal match When there is a successful match, stop the match and process the request according to the current match rule

Note: prefix matching, if there is an inclusion relationship, matches according to the maximum matching principle. For example, in prefix matching: location /dir01 with location /dir01/dir02 , if requested http://localhost/dir01/dir02/file Will eventually match to location /dir01/dir02

The example has the following matching rules:


location = / {
  echo " The rules A";
}
location = /login {
  echo " The rules B";
}
location^~ /static/ {
  echo " The rules C";
}
location^~ /static/files {
  echo " The rules X";
}
location ~ \.(gif|jpg|png|js|css)$ {
  echo " The rules D";
}
location ~* \.png$ {
  echo " The rules E";
}
location /img {
  echo " The rules Y";
}
location / {
  echo " The rules F";
}

The effect is as follows:

Access the root directory / , such as http://localhost/ The rule A will be matched access http://localhost/login Will match the rule B, http://localhost/register Matches the rule F access http://localhost/static/a.html The rule C will be matched access http://localhost/static/files/a.exe Rule X will be matched. Although rule C can also be matched, rule X is finally selected because of the maximum matching principle. You can test that by removing rule X, the current URL will match rule C. access location /dir01/dir02 0 , http://localhost/b.jpg Rule D and rule E will be matched, but rule D order takes precedence, rule E does not work, and http://localhost/static/c.png Matches to rule C first access http://localhost/a.PNG Matches rule E instead of rule D, because rule E is not case sensitive. access http://localhost/img/a.gif Rule D will be matched. Although rule Y can also be matched, rule Y is ignored because regular matching takes precedence. access http://localhost/img/a.tiff It will match the rule Y.

access http://localhost/category/id/1111 In this case, Nginx should forward the request to the back-end application server, such as FastCGI (php), tomcat (jsp) and Nginx as the reverse proxy server.

Therefore, in practice, the author believes that there are at least three definitions of matching rules, 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 modes, directory matching or suffix matching, whichever you choose 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/
}

rewrite grammar

The last works mostly with this Flag The break wok has aborted Rewirte and will no longer match The redirect wok returns the temporary redirected HTTP status 302 The permanent wok returns permanent redirection to HTTP state 301

1. The following expressions can be used to judge:

- f and! -f is used to determine if a file exists - d and! -d is used to determine if a directory exists - e and! -e is used to determine if a file or directory exists - x and! -x is used to determine whether the file is executable

2. The following are the global variables that can be used for judgment

Ex. :


http://localhost:88/test1/test2/test.php?k=v
$host : localhost
$server_port : 88
$request_uri : /test1/test2/test.php?k=v
$document_uri : /test1/test2/test.php
$document_root : D:\nginx/html
$request_filename : D:\nginx/html/test1/test2/test.php

redirect grammar


server {
  listen 80;
  server_name start.igrow.cn;
  index index.html index.php;
  root html;
  if ($http_host !~ "^star\.igrow\.cn$") {
    rewrite^(.*) http://star.igrow.cn$1 redirect;
  }
}

Preventing hotlinking


location ~* \.(gif|jpg|swf)$ {
  valid_referers none blocked start.igrow.cn sta.igrow.cn;
  if ($invalid_referer) {
    rewrite^/ http://$host/logo.png;
  }
}

Set the expiration time based on the file type


location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
  if (-f $request_filename) {
    expires 1h;
    break;
  }
}

Related articles: