location Matching for Nginx &&rewrite

Keywords: Web Development Linux Nginx regex

1. Common Nginx regular expressions

It's also mentioned in the blog before the rule, and it's important to mention it here.

^ : Matches the starting position of the input string
$ : Match end position of input string
* : Match the previous character zero or more times. Such as " ol*"Can match " o"And " ol","oll"
+ : Match the previous character one or more times. For example, " ol+"Can match " ol"And " oll","olll",But it doesn't match " o"
? : Match the previous character zero or once, for example, " do(es)?"Can match " do"Or " does","?"Equivalent to"{0,1}"
. : Match Division\n"To match any single character other than "\n"Any character, such as "[.\n]"Such a pattern
\ : Mark the following character as a special character or a literal character or a backward reference. For example, "\n"Matches a line break, and "\$"Match " $"
\d : Match Pure Numbers
{n} : repeat n second
{n,} : repeat n Second or more times
{n,m} : repeat n reach m second
[] : Define matching character ranges
[c] : Match a single character c
[a-z] : matching a-z Any one of the lowercase letters
[a-zA-Z0-9] : Match all upper and lower case letters or numbers
() : Start and end of expression
| : OR operator

2. location

1. Locations can be roughly divided into three categories

  • Precision matching: location = / {}
  • General match: location / {}
  • Regular matching: location ~/ {}

2. Matching rules commonly used by location s

=   :Perform an exact match of common characters, that is, a perfect match.
^~  :Represents a normal character match. Prefix matching is used. If the match is successful, no other matches are made. location. 
~   :Case sensitive matching.
~*  :Case insensitive matching.
!~  :Case-sensitive matches are negated.
!~* :Case insensitive matches are negated.

3. location Priority

Firstly, match exactly =
Secondary prefix matching ^~
Next is regular matching in file order ~or~*
Then match the prefix without any modification
 Last but not least / Universal Matching

4. Example description of location

(1)location = / {}
=For exact matching / ,Host name cannot be followed by any string, such as access / and /data,be / Match,/data Mismatch
 Another example location = /abc,Then only match/abc ,/abc/or /abcd Does not match. If location  /abc,Then match/abc ,/abcd/ Also match /abc/. 

(2)location / {}
Because all addresses are / Beginning, so this rule will match all requests such as access / and /data, be / Match, /data Match also,
But if followed by a regular expression, it will first match the longest string (longest match)

(3)location /documents/ {}
Match any with /documents/ Beginning address, when matched, continue searching for others location
 Only others location This is only used if the following regular expressions do not match

(4)location /documents/abc {}
Match any with /documents/abc Beginning address, when matched, continue searching for others location
 Only others location This is only used if the following regular expressions do not match

(5)location ^~ /images/ {}
Match any with /images/ Use this rule to stop searching down after matching the starting address

(6)location ~* \.(gif|jpg|jpeg)$ {}
Match all with gif,jpg or jpeg Ending Request
 However, all requests /images/ The picture below will be location ^~ /images/ Processing because ^~ Priority is higher, so this rule cannot be reached

(7)location /images/abc {}
Maximum characters match to /images/abc,Lowest priority, continue searching for others location,You will find ^~ and ~ existence

(8)location ~ /images/abc {}
Match with/images/abc At the beginning, priority is next, only removed location ^~ /images/ That's what happens

(9)location /images/abc/1.html {}
matching/images/abc/1.html File, if and regular ~ /images/abc/1.html Regular priority is higher than

Summary of priorities:
(location =) > (location full path) > (location ^~path) > (location, * regular order) > (location part start path) > (location /)

5. There are at least three matching rule definitions in actual site usage
First required rule

Direct matching of site roots, access to the home page of the site through the domain name is more frequent, use this will speed up processing, such as the official website.
This can be forwarded directly to the back-end application server, or it can be a static home page
location = / {
    proxy_pass http://tomcat_server/;
}

Second required rule

Processing static file requests, this is nginx As http Server strengths
 There are two configuration modes, directory matching or suffix matching,Use either or in combination
location ^~ /static/ {
    root /webroot/static/;
}

location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

Third Rule

Common rules, such as to forward bands.php,.jsp Dynamic requests for suffixes to back-end application servers
 Non-static file requests default to dynamic requests
location / {
    proxy_pass http://tomcat_server;
}

3. rewrite

The rewrite function is to rewrite and redirect URL s using global variables provided by nginx or variables set by nginx itself, combined with regular expressions and tags. For example:
You need to keep the old domain name going to the new domain name after changing the domain name
A change in a page requires a jump to a new page
Website Anti-theft Chain
And so on.
Note: rewrite s can only be placed in server {}, location {}, if {}, and by default only work on strings after domain names other than passed parameters.
For example: http://www.lisi.com/a/we/index.php?id=1&u=str Rewrite / a/we/index.php.

1. rewrite jump implementation

Nginx: through ngx_ Http_ Rewrite_ The module supports URL rewriting and if conditional judgment, but does not support else
Jump: Jump from one location to another, the loop can be executed up to 10 times, after which nginx will return 500 errors
PCR E support: grammar rule matching for perl-compatible regular expressions
Override module set directive: Create and assign a new variable

2. rewrite Execution Order

First priority: execute the rewrite command inside the server block
Second priority: Perform location matching
Third priority: execute the rewrite directive in the selected location

3. rewrite Syntax Format

Grammar rewrite;
regex:Represents a regular match rule
replacement:Indicates what has been jumped
flag: flag tag indicating rewrite support

4. flag tag description

last: After this rule has been matched, continue to match down the new location URI rule, commonly used in server s and if.
break: This rule terminates when the match is complete and no longer matches any of the following rules, which are generally used in location s.
redirect: Returns 302 temporary redirects, and the browser address displays the URL address after the jump.
Permanent: Returns 301 permanent redirection, and the browser address bar shows the URL address after the jump.

5. rewrite example

1) Domain Name Based Jump
Now the company's old domain name www.test.com has changed business requirements and needs to be replaced by the new domain name www.lisi.com. However, the old domain name cannot be abolished and needs to jump to the new domain name with the following parameters unchanged.

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.test.com;
	charset utf-8;
	access_log  /var/log/nginx/www.test.com.access.log;
	location / {
	    if ($host = 'www.test.com'){
	        rewrite ^/(.*)$ http://www.lisi.com/$1 permanent;
	    }
        root   html;
        index  index.html index.htm;
    }
}

echo "192.168.206.11 www.lisi.com www.test.com" >> /etc/hosts

systemctl restart nginx

Browser Input Simulated Access http://www.test.com/1.html
Will jump to www.lisi.com/1.html, look at the element you can see returns 301, achieve a permanent redirection jump, and the parameters after the domain name also jump normally.

(2) Client IP Access Jump

Today, the new version of the company's business comes online, requiring all IP access to display a fixed maintenance page for any content, with only company IP: 192.168.206.11 accessing normally.

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.test.com;
	charset utf-8;
	access_log  /var/log/nginx/www.test.com.access.log;

	#Set the legal IP flag; set the variable $rewrite with a boole value of true
    set $rewrite true;
    #Determine whether it is a legitimate IP; When the client IP is 192.168.206.11, set the variable value to false and do not override it
    if ($remote_addr = "192.168.206.11"){
        set $rewrite false;
    }
	#All but legitimate IP are illegal, rewrite skip maintenance page
	#Override when variable value is true
    if ($rewrite = true){
        #Override Insert/weihu.html after accessing IP, for example 192.168.206.11/weihu.html
        rewrite (.+) /weihu.html;
    }
    location = /weihu.html {
        #Page returns / var/www/html/weihu.html content
        root /var/www/html;
    }
	location / {
        root   html;
        index  index.html index.htm;
    }
}
mkdir -p /var/www/html/
echo "<h1>As server maintenance, please visit later, thank you.</h1>" > /var/www/html/weihu.html
echo "192.168.206.11 www.test.com" >> /etc/hosts
systemctl restart nginx

Browser Access
Only IP 192.168.206.11 can be accessed properly, other addresses are maintenance pages

3) Jump to a new domain name followed by a directory based on the old domain name

Now you are visiting http://lisi.test.com Now you need to jump to all the access below this domain name http://www.test.com/lisi

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  lisi.test.com;
	charset utf-8;
	access_log  /var/log/nginx/lisi.test.com.access.log;
	#Add to; Here, $1 is the location variable, representing/post
	location /post {
        rewrite (.+) http://www.test.com/lisi$1 permanent;
    }
	location / {
        root   html;
        index  index.html index.htm;
    }
}
mkdir -p /usr/local/nginx/html/lisi/post
echo "this is 1.html" > /usr/local/nginx/html/lisi/post/1.html
echo "192.168.206.11 lisi.test.com www.test.com" >> /etc/hosts
systemctl restart nginx.service

Use browser access
http://lisi.test.com/post/1.html Jump To http://www.test.com/lisi/post/1.html


(4) Jump based on parameter matching

Visit now http://www.test.com/100-(100|200)-100 (any number). html Jump to http://www.test.com Page.

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.test.com;
	charset utf-8;
	access_log  /var/log/nginx/www.test.com.access.log;

    if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        rewrite (.+) http://www.test.com permanent;
    }

    location / {
        root   html;
        index  index.html index.htm;
    }
}

echo "192.168.206.11 www.test.com" >> /etc/hosts
systemctl restart nginx

Browser Access
http://www.test.com/100-200-100.html or
http://www.test.com/100-100-100.html Jump To http://www.test.com Page.

(5) File jumps based on all php endings in the directory

Request access http://www.test.com/upload/abc.php Jump to the first page.

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.test.com;
	charset utf-8;
	access_log  /var/log/nginx/www.test.com.access.log;
	
location ~* /upload/.*\.php$ {
    rewrite (.+) http://www.test.com permanent;
}

location / {
    root   html;
    index  index.html index.htm;
}
}

echo "192.168.206.11 www.test.com" >> /etc/hosts
systemctl restart nginx

Browser Access
http://www.test.com/upload/abc.php Jump To http://www.test.com Page.

summary

From a functional point of view, rewrite and location seem to be a bit similar, and both can be jumped. The main difference is that
rewrite is in the same domain
location is a control access or reverse proxy to a class of paths and can also proxy_pass to other machines

Posted by pngtest on Mon, 18 Oct 2021 09:21:42 -0700