Example of a method for Nginx location matching rules

  • 2020-05-15 03:35:12
  • OfStack

1, grammar,


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

2, description,

From the above grammar, we can know that location can be divided into three parts, and the next study is 1 for each part.

1) [=|~|~*|^~|@]

= : represents the exact match of url ~ : represents a regular match, but is case sensitive ~* : regular matching, case insensitive ^~ : indicates normal character matching, if this option matches, only this option matches, do not match other options, 1 generally used to match the directory @ : "@" defines a named location for internal orientation, such as error_page

There are several different symbols defined to represent different matching rules. What about the order of precedence?

The directive with the = prefix matches the query exactly. If found, stop searching; All the remaining normal strings, the longest match. If this match USES the ^~ prefix, the search stops; Regular expressions, the order defined in the configuration file; If rule 3 produces a match, the result is used. Otherwise, use the result of rule 2.

Test example 1:


location = /world {
 return 600;
}

location = /hello {
 return 600;
}

location ~ /hellowo {
 return 602;
}

location ^~ /hello {
 return 601;
}


-  request  localhost/world  return 600
-  request  localhost/world2 localhost/test/world  Return to the other 
-  request  localhost/hello  return 600
-  request  localhost/hello/123  return 601
-  request  localhost/hellow  return 601
-  request  localhost/hellowo  return 601
-  request  localhost/test/hellowo  return 602
-  request  localhost/test/hello  Return to the other 

So we can know:

= is an exact and complete match with the highest priority; When regular matching, if ~ and ^~ match the rules at the same time, ^~ takes precedence; ^~ this rule does not match the following path in the request url, as shown above /test/hello does not match ^~ does not support regular, compared with =, the scope is wider, hellowo can be ^~ match, but = will not match; The ~ path matches as long as it contains, and /test/hellowo returns 602 as shown above

Test example 2:


location ~ /hello {
 return 602;
}

location ~ /helloworld {
 return 601;
}


-  request  localhost/world/helloworld  return  602
-  request  localhost/helloworld  return  602

Adjust the order above


location ~ /helloworld {
 return 601;
}

location ~ /hello {
 return 602;
}


-  request  localhost/helloworld  return 601
-  request  localhost/world/helloworld  return 601
-  request  localhost/helloWorld  return 602

So at the same time the regular match

Put the first match in front Note that if case - insensitive, use ~* Try to put the exact match in front of you

Test example 3:


location ^~ /hello/ {
 return 601;
}

location /hello/world {
 return 602;
}

In this scenario, there is a routing rule that does not conform, so what is the actual test?


- http://localhost/hello/wor  return 601
- http://localhost/hello/world  return 602
- http://localhost/hello/world23  return 602
- http://localhost/hello/world/123  return 602

As you can see from the example above

Full match is preferred when there is no match

2) [uri]

Here, we mainly fill in the path path that needs to match. According to the previous symbol, we can fill in the path path, or we can fill in the regular expression. The following part mainly explains the regularity

. : matches any character other than a newline The & # 63; : repeat 0 or 1 times + : repeat 1 or more times * : repeat 0 or more times \d: matching Numbers ^ : matches the beginning of the string $: an introduction to matching strings {n} : repeat n times {n,} : repeat n or more times [c] : matches a single character c [a-z] : matches any 1 lowercase letter of a-z The match between the parentheses () can be referred to later by $1, and $2 represents the first (). The confusing thing inside the regular is that \ escapes special characters.

Routing forwarding

Requesting an path match is only step 1. After the match, how do you forward the request to another web service?

1. Reverse proxy

One commonly seen use is to use the nginx proxy request and forward it to other web services internally

It is mainly implemented through prixy_pass


location ^~ /webs {
 proxy_pass http://127.0.0.1:8080/webs;
}

The implication of the above rule is to forward all requests beginning with webs to the web service on port 8080.

The above is to directly write dead forward to 1 ip, if more than one machine to provide service, can be configured like this


location = /world {
 return 600;
}

location = /hello {
 return 600;
}

location ~ /hellowo {
 return 602;
}

location ^~ /hello {
 return 601;
}

0

2. Rewrite command

The function of rewrite is to use the global variables provided by nginx or the variables set by nginx to achieve url rewriting and redirection in combination with regular expressions and flag bits.

rewrite can only be placed in server{},location{},if{}, and can only work on strings that follow the domain name except for passed parameters, such as

http://ofstack.com/a/we/index.php?id=1&u=str

Only to/a we/index php rewritten.

Grammar: rewrite regex replacement [flag];

Example:


location = /world {
 return 600;
}

location = /hello {
 return 600;
}

location ~ /hellowo {
 return 602;
}

location ^~ /hello {
 return 601;
}

1

Will hello beginning, all forwarded to/hexo public/index html


Related articles: