Nginx Rewrite + regular expression

Keywords: Operation & Maintenance network Nginx regex cloud computing

preface

Now Nginx has become the first choice for many companies as front-end proxy pass servers. In practical work, they often encounter a lot of requirements for jump (Rewriting URL). For example, after changing the domain name, you need to keep the old domain name jump to the new domain name, change a web page and jump to a new page, website anti-theft chain and so on. If the Apache server is used at the back end, although it can also jump and the rule base is very powerful, the jump efficiency with Nginx will be higher.

1, Nginx Rewrite overview

1.Rewrite jump scene

  • Adjust the URL users browse to look more standardized and meet the needs of developers and product personnel.
  • In order to make the search engine search and record website content and user experience better, enterprises will disguise the dynamic URL address as a static address to provide services.
  • After the URL is changed to a new domain name, let the old access jump to the new domain name. For example, visiting 360buy.com of jd.com will jump to jd.com.
  • Some business adjustments of the server, such as URL adjustment according to special variables, directories and client information

2. Rewrite jump implementation

  • Nginx is through NGX_ http_ rewrite_ The module module supports url rewriting and if condition judgment, but does not support else.
  • This module requires PCRE support. PCRE support should be specified when compiling Nginx. It is installed by default. According to the redirection of related variables and different configurations, jump from one location to another. However, such a cycle can be executed up to 10 times. After that, Nginx will return 500 errors.
  • At the same time, the rewrite module contains a set instruction to create a new variable and set its value, which is very useful in some situations, such as recording condition identification, passing parameters to other location s, recording what has been done, etc.
  • The rewrite function is to rewrite and redirect the URL using the global variables provided by Nginx or the variables set by yourself, combined with regular expressions and flag bits

3.Rewrite actual scenario

1. Implementation of nginx jump requirements

  • Match jump using rewrite

  • Jump after matching global variables with if

  • Use location to match and then jump

2.rewrite is placed in the server {}, if {}, location {} segments

  • location only works on the string after the domain name except the passing parameters - the path of the page file

3. For domain name or parameter string

  • Use if global variable matching
  • Using proxy_pass reverse proxy

2, Nginx regular expression

1. Common regular expression metacharacters

Character description
^Matches the starting position of the input string
$matches the end of the input string
*Matches the preceding character zero or more times
+Matches the preceding character one or more times
? Matches the preceding character zero or once
. match any single character except "\ n"
\Mark the following characters as a special character or a literal character or a backward reference
\d matches pure numbers
{n} Repeat n times
{n,} repeat n or more times
[c] Match single character c
[a-z] matches any of the a-z lowercase letters
[a-zA-Z] match any one of A-Z lowercase letters or A-Z uppercase letters

2. Differences between nginx and apache

Regular expressions are one of the differences between the two
Regular expression: it can more accurately match the required string / parameter / position, etc

3, Detailed explanation of Rewrite command

1. Syntax format

rewrite <regex> <replacement> [flag];

#< regex >: regular expression        
#< replacement >: content after jump     
#[flag]rewrite supported flag flags

2.flag mark

Marking description
last is equivalent to Apache's [L] tag, indicating completion of rewrite
break this rule will terminate upon completion of matching and will no longer match any subsequent rules
redirect returns 302 temporary redirection, the browser address will display the URL address after the jump, and the crawler will not update the URL
Permanent returns 301 permanent redirection, the browser address will display the URL after jump, and the crawler updates the URL

  • Comparison between last and break
1lastbareak
Usage scenarioIt is usually written in server and ifGenerally, it is used in location
URL matchDo not terminate rewritten URL matchingTerminate rewritten URL matching
  • last: after the url is rewritten, immediately initiate a new request, enter the server block again, and retry the location matching. If the matching fails for more than 10 times, 500 errors are reported, and the address bar remains unchanged;
  • break: after the url is rewritten, the current resource is directly used instead of the remaining statements of location. The request is completed and the address bar remains unchanged
  •  in general: last and break After the reset, the address bar will not change, which is their same point. The difference is last Will write in server and if In, break It's written in location In, last Rewritten will not be terminated url Match, break Will terminate the rewritten url Match.
    

4, location resolution

1.location classification

location = patt {}  [Accurate matching]   			 #Exact match string
location patt {}    [General matching]				 #Just a string containing pat
location ~ patt {}  [Regular matching]				 #Match by regular expression

2. Common expressions for regular matching

~Performs a regular match, case sensitive
~*Performs a regular match, case insensitive
!~ Perform a regular match, case sensitive, mismatch
!~* Performs a regular match, case insensitive
^~For normal character matching, prefix matching is used. If the matching is successful, other location s will not be matched
=Exact matching of ordinary characters, that is, exact matching
@Define a named location, which is used in internal orientation

3.location priority

  • For expressions of the same type, the long string will be matched first
  • Match by priority
= type                                     			 #First, exact matching
^~ Type expression								  			 #Secondly, prefix matching
 Regular expression(~and~*)type					  			 #The second is regular matching according to the order in the file
 General string matching type, matching by prefix				   			 #Then match the prefix without any modification	
Universal matching(/),If there is no other match, any request will match 		 	 #Finally to / general matching

4. Priority example

-------------------------------------------------------------------------------------------------
location = / {                     
[configuration A]
}
#Exact matching. The host name cannot be followed by any string. For example, if you access / and / data, then / matches and / data does not match;
such as location = /abc,Then only match/abc,/abc/or/abcd Mismatch;
if location /abc,Then match/abc,/abcd/ Also match/abc/
-------------------------------------------------------------------------------------------------
location / {                     
[configuration B]
}
#Generally, all addresses start with /. This rule will match all requests, such as accessing / and data, then / matches and / data also matches. However, if regular expressions and the longest characters are followed, priority will be given to matching
------------------------------------------------------------------------------------------------
location /documents/ {            
[configuration C]
}
#Match any address starting with / documents /. After matching, continue to search for other location s. This item will be used only when the following regular expressions do not match
------------------------------------------------------------------------------------------------
location ~ /documents/abc {     
[configuration D]
}
#Match any address starting with / documents/abc. After matching, continue to search for other location s. This item will be used only when the following regular expressions do not match
------------------------------------------------------------------------------------------------
location ^~ /images/ {     
[configuration E]
}
#Match any address starting with / images /. After matching, stop matching
------------------------------------------------------------------------------------------------
location ~* \.(gif|jpg|jpeg)$ {    
 [configuration F]
}
#Match all requests ending in gif,jpg or jpeg because ^ ~ has the highest priority
------------------------------------------------------------------------------------------------
location ~ /images/abc {           
 [configuration G]
 }
#If the matching starts with / images/abc, the priority level takes the second place. This item will be adopted only when location ^~ /images / is removed
------------------------------------------------------------------------------------------------
 location /images/abc/1.html {     
 [configuration H]
}
 #Match the / images/abc/1.html file. If compared with regular location ~ /images/abc/1.html, regular has the highest priority
 ----------------------------------------------------------------------------------------------
 location /images/abc {             
[configuration I] 
 }
 #The longest character matches / images/abc, with the lowest priority. Continue to search for other location s, and you will find that ^ ~ and ~ exist
 ----------------------------------------------------------------------------------------------

5. Comparison between rewrite and location

  •   Same point: jump can be realized
    
  •   difference:① rewrite Is to change the path to obtain resources within the same domain name② location It is a kind of path control access or reverse proxy, and can also proxy_pass To other machines
    
  •    rewrite Will write in location In, execution sequence①implement server Inside the block rewrite instructions②implement location matching③Execute the selected location Medium rewrite instructions
    

6.location priority rule

  •       ① that location What are the priorities?
    
  • Match a specific file
(location = (full path)>(location ^~ (full path)>(location ~* (full path)>(location ~ (full path)>(location Full path)>(location /)

  • Access a file with a directory match
(location = Table of contents)>(location ^~ catalogue/)>(location ~ catalogue)>(location ~* Table of contents)>(location Table of contents)( location /)

  •       ② Why do files and directories change only in case insensitive areas?
    
  • The purpose of regular expressions is to match as accurately as possible
  • File, match as accurately as possible, and it is easier to find if it is not distinguished
  • The directory shall be matched as accurately as possible, case sensitive, with higher priority and easier to find
  •  ③ In actual website use, there must be at least three matching rule definitions    
    
-------------------------------------------------------------------------------------------------
#The first required rule: directly match the website root. Visit the website home page through the domain name more frequently (www.baidu.com /), and using this will speed up the processing, such as the official website;
It can be a static home page, or it can be directly forwarded to the back-end application server
location = / {
	root	html ;
	index index.html index.htm;
}
-------------------------------------------------------------------------------------------------
#The second required rule: handle static file requests, which is the strength of nginx as an http server;
There are two configuration modes, directory matching or suffix matching. Choose one or use it together
location ^~ /static/ {
	root /webroot/static/ ;
}
location ~* \. (html|gif|jpg|jpeg|png)$ {
	root /webroot/res/ ;
} 
------------------------------------------------------------------------------------------------
#The third mandatory rule is the general rule, for example, it is used to forward dynamic requests with. php and. jsp suffixes to the back-end application server,
Non static file requests are dynamic requests by default,That is, jump or reverse proxy
upstream	tomcat_ server {
	192.168.8.135:80
	192.168.8.132:80
location / {
	proxy_pass http://tomcat_server;
}
------------------------------------------------------------------------------------------------

5, Case analysis

1. Domain name based jump

  •      Old company domain name www.gkd.com Due to changes in business requirements, a new domain name needs to be used www.haha.com Instead, the old domain name cannot be abolished, and the old domain name can jump to the new domain name, and the following parameters remain unchanged.
    
  • ① Add mapping and start nginx service
[root@localhost ~]#systemctl status nginx.service 
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since VI 2021-10-09 00:30:45 CST; 3 days ago

[root@localhost ~]#vim /etc/hosts
......
192.168.8.135 www.gkd.com www.haha.com

  • ② Create a log directory and modify the main configuration file
[root@localhost ~]#mkdir -p /var/log/nginx
[root@localhost ~]#vim /usr/local/nginx/conf/nginx.conf
......
	server {
	    listen       80;
        server_name  www.gkd.com;                  		 #Modify domain name
        charset utf-8;									 #The character set can be modified optionally
        access_log  /var/log/nginx/www.gkd.com.log; 	 #Open and modify the log saving path
	    location / {									 #Insert the following content in the original location
			if ($host = 'www.gkd.com'){						 #$host is a rewrite global variable, indicating the request host header field or host name
			    rewrite ^/(.*)$ http://www.haha.com/$1 permanent; 	#$ 1 is the matching location variable, that is, the string after the domain name, and it will jump permanently at the same time
			}                    							 
		root   html;
		index  index.html index.htm;
	    }
......
	}
......

[root@localhost ~]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]#systemctl restart nginx.service 

  • ③ Verify with browser
  • We can see that when you visit www.gkd.com, you will automatically jump to www.haha.com for access, and when you visit www.gkd.com/1.txt, you can see that the domain name has also changed to www.haha.com/1.txt when the error page is displayed.

2. Access jump based on ip address

  •                    Since the new version of the company's business is online, all IP Access to any content will display a fixed maintenance page, only the company's own IP Address 192.168.8.135 For normal access.
    
  • ① Create a directory of maintenance pages and add content
[root@localhost /usr/local/nginx/html]#mkdir -p /var/www/html
[root@localhost /var/www/html]#vim weihu.html
 Website maintenance in progress~~~~~~~~~~

  • ② Master profile configuration
......
server {
listen       80;
server_name  www.gkd.com;
charset utf-8;
access_log  /var/log/nginx/www.gkd.com.log;      #Open and modify the log saving path
set $rewrite true;								 #Set the variable $rewrite. The variable value is Boolean and the value is true
if ($remote_addr = "192.168.8.135"){	#When the client ip is 192.168.8.135, set the variable value to flash without rewriting
set $rewrite false;
}										#In addition to the legal IP, the others are illegal IP. Rewrite them and jump to the maintenance page
if ($rewrite = true){                   #Boolean expressions will match location s that satisfy true if they do not satisfy false
rewrite (.+) /weihu.html;				#Rewrite: Add / weihu.html after accessing the IP, such as 192.168.8.135/weihu.html
}
location = /weihu.html {
root /var/www/html;						#The page returns the contents of / var/www/html/weihu.html
}
location / {
root   html;
index  index.html index.htm;
}
......

  • ③ Check the configuration file syntax and restart the service
[root@localhost /usr/local/nginx/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /usr/local/nginx/html]#systemctl restart nginx.service 

  • ④ Open the browser to verify

  •                   At this time, access is made with another machine 192.168.8.135 At this address, you will find that the page has jumped to the maintenance page, and only IP The address is 192.168.8.135 This machine can only be accessed normally
    

3. Based on the domain name, jump to the new domain name followed by the directory

  •                 Requirement: when the domain name accessed is http://bbs.gkd.com/post/1.html automatically jumps to http://www.haha.com/bbs/post/1.html
    
  • ① Create a post web page file in the bbs directory and add content
[root@localhost /var/www/html]#mkdir -p /usr/local/nginx/html/bbs/post
[root@localhost /var/www/html]#vim /usr/local/nginx/html/bbs/post/1.html
[root@localhost /var/www/html]#cat /usr/local/nginx/html/bbs/post/1.html 
<h1>Document toxic~~~~~~</h1>
[root@localhost /var/www/html]#vim /etc/hosts
192.168.8.135 bbs.gkd.com www.haha.com

  • ② Master profile configuration
.......
server {
listen       80;
server_name  bbs.gkd.com;								#rename domain					
charset utf-8;
access_log  /var/log/nginx/www.gkd.com-access.log;      #Open and modify the log saving path
location /post {										#Add location; match starting with post
rewrite (.+) http://Www.haha.com/bbs $1 permanent; # $1 is the location variable, representing / post
}
location / {
root   html;
index  index.html index.htm;
}
......

  • ③ Check the main configuration file syntax and restart the service
[root@localhost /var/www/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /var/www/html]#systemctl restart nginx.service 

  • Open the browser to verify
  •               You can see that the domain name has automatically jumped to http: //www.haha.com/bbs/post/1.html    
    

4. Jump based on parameter matching (redundant)

  •             Requirements: access http://www.gkd.com/100-(100|200)-100.html will jump to http://www.gkd.com Page of        
    
  • ① Master profile configuration
[root@localhost /var/www/html]#vim /usr/local/nginx/conf/nginx.conf
......
server {
listen       80;
server_name  www.gkd.com;							#rename domain
charset utf-8;
access_log  /var/log/nginx/www.gkd.com-access.log;  #Open and modify the log saving path
#Set regular matching, for example: http://www.gkd.com/100-100-123.html													
if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$){   #$repuest_uri built-in variable, representing URI, \ d pure number
rewrite (.*) http://www.gkd.com permanent; 			# Set override
}
location / {
root   html;
index  index.html index.htm;
}
......

  • ② Check the main configuration file syntax and restart the service to configure the mapping
[root@localhost /var/www/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /var/www/html]#systemctl restart nginx.service 
[root@localhost /var/www/html]#vim /etc/hosts
192.168.8.135 www.gkd.com

  • ③ Verify with browser
  •                You can see the access domain name http://www.gkd.com/100-100-100.html http://www.gkd.com  Page access for http://www.gkd.com/100-200-100.html  You can also jump
    

5. Jump to all php ending files based on the directory

  •          Requirements: access http://www.gkd.com/upload/123.php jump to the home page (common scenario: new users of the website prompt to register)
    
  •   ① Master profile configuration
    
[root@localhost /var/www/html]#vim /usr/local/nginx/conf/nginx.conf
......
server {
listen       80;
server_name  www.gkd.com;
charset utf-8;
access_log  /var/log/nginx/www.gkd.com-access.log;
location ~* /upload/.*\.php$ {         
# ~*It indicates regular matching, case insensitive, and matches all files ending in. php in the / upload directory
rewrite (.*) http://www.gkd.com permanent;
}
location / {
root   html;
index  index.html index.htm;
}
......



  • ② Check the main configuration file syntax and restart the service
[root@localhost /var/www/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /var/www/html]#systemctl restart nginx.service 

  • ③ Open the browser to verify
  •                  Can see access http://www.gkd.com/upload/123.php will jump to http://www.gkd.com  home page
    

6. Jump based on the most common url request

  •                      Requirements: visit a specific page, such as: http://www.gkd.com/abc/123.html, you can jump to the home page
    
  • ① Master profile configuration
[root@localhost /var/www/html]#vim /usr/local/nginx/conf/nginx.conf
......
    server {
        listen       80;
        server_name  www.gkd.com;
        charset utf-8;
        access_log  /var/log/nginx/www.gkd.com-access.log;
        location ~* /abc/123.html {                     
                rewrite (.+) http://www.gkd.com permanent;
        }
        location / {
            root   html;
            index  index.html index.htm;
        }

......

  • ② Check the main configuration file syntax and restart the service
[root@localhost /var/www/html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost /var/www/html]#systemctl restart nginx.service 

  • ③ Verify with browser
  •                          Can see access http://Go to www.gkd.com/abc/123.html and go to the main page
    

Posted by landysaccount on Wed, 10 Nov 2021 06:05:24 -0800