Nginx Rewrite overview classification and priority + experiment of various application scenarios

1. Overview of Nginx Rewrite

1.1 Overview of Nginx Rewrite

Now Nginx has become the first choice of many companies as a front-end reverse proxy server. In actual work, they often encounter many requests for redirection (URL rewriting). For example, after changing the domain name, you need to keep the old domain name to be able to jump to the new domain name, a certain webpage needs to jump to a new page after a change, the website anti-theft chain, and so on. If the Apache server is used in the backend, although it can also do jumps, the rule base is also very powerful, but the Nginx jump efficiency will be higher.

1.2, Rewrite jump scene

(1) The URL that users browse can be adjusted, which looks more standardized and meets the needs of developers and product personnel

(2) In order to make search engines search for website content and better user experience, companies will disguise dynamic URL addresses as static addresses to provide services

(3) After the URL is changed to a new domain name, let the old visits jump to the new domain name

(4) Adjust the URL according to special variables, directories, and client information

1.3, Rewrite jump implementation

Insert picture description here

1.4, Rewrite actual scene

The implementation of Nginx jump requirements:
use rewrite for matching jump,
use if to match global variables and then jump,
use location to match and then jump

Rewrite is placed in the server{}, if{}, location{} section,
location only works on the string after the domain name except for the passed parameters


Use if global variable matching for domain name or parameter string
Use proxy_pass reverse proxy

1.5, Nginx regular expression

Insert picture description here

1.6, Rewrite command

Rewrite command syntax:
rewrite <regex> <replacement> [flag]
regex: regular expression
replacement: content after the jump
flag: flag supported by rewrite

Flag description:
Insert picture description here
Comparison of last and break:
Insert picture description here

1.7 Location classification

ocation= patt {} [exact match]
location patt {} [general match]
location ~ patt {} [general match]

Common expressions for regular matching
Insert picture description here

1.8, location priority

Expressions of the same type, the longer string will be matched first

In order of priority:

= Type
^~ type expression
regular expression (and *) type
regular string matching type, matching by prefix
general matching (/), if there is no other match, any request will be matched

Location priority rule:
match a specific file
(location=full path)> (location ~ full path)> (location ~ *full path) >(location~full path)> (location full path)> (location /)

Access a file with directory matching
(location=directory)> (location ^ ~ directory/)> (location ~ directory)> (location~*directory)> (location directory)> (location /)

Two, Rewrite application scenario experiment

The environment is based on nginx has been installed

2.1, redirect based on domain name

The company’s old domain name www.51xit.top, due to changes in business requirements, needs to use the new domain name www.52xit.top instead

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
    
    
            root   html;
            index  index.html index.htm;
              #### 新增部分 ####
              if ($host = 'www.51xit.top') {
    
      ##这边是老域名
                rewrite ^/(.*)$ http://www.52xit.top/$1 permanent;    
                ##使用正则表达式匹配 “/开头和任意字符结尾”,然后跳转到新的www.52xit.top
                }
             }
[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 ~]# killall -s HUP nginx

On the real machine, open the browser and enter www.51xit.top to jump to www.52xit.top
Insert picture description here
Insert picture description here

2.2. Access redirect based on client IP

Access redirect based on client IP. For example, today the new version of the company’s business is online, all IP accesses will display a fixed maintenance page, and only the company’s IP can be accessed normally

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
    
    
        listen       80;
        server_name  www.51xit.top;    #改域名
        charset utf-8;               #改字符集
        access_log  /usr/local/nginx/logs/www.51xit.top.access.log;  #改日志存储路径 去掉main
        set $rewrite true;   #设置是否合法的IP标志
       
        if ($remote_addr = "20.0.0.11"){
    
       #改客户机的IP地址
                set $rewrite false;
        }
        if ($rewrite = true){
    
    
                rewrite (.*) /wh.html;  #跳转到wh.html
        }
        
        location = /wh.html {
    
    
                root /usr/local/nginx/html;  ##存放路径
        }
        
        location / {
    
    
           root html;
           index index.html index.htm;
       }
[root@localhost ~]# killall -s HUP nginx   #重启
[root@localhost ~]# echo "网页维护中,请联系管理员" >/usr/local/nginx/html/wh.html

Create web page

[root@localhost ~]# cp index.html wh.html   ##创建维护页面
[root@localhost ~]# vi wh.html 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
    
    
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>网页维护中,暂时无法访问!!!</h1>
</body>
</html>
== >> wq 保存
[root@localhost ~]# killall -s HUP nginx

On the real machine, open the browser and enter 20.0.0.25, it will show that the webpage is under maintenance and cannot be accessed temporarily
Insert picture description here

2.3. Redirect based on old and new domain names

Based on the old domain name jump to the new domain name followed by the directory, the entry is now http://www.51xit.top/bbs and
now you need to transfer all under this domain name to http://www.52xit.top/new/ bbs/

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
    
    
        listen       80;
        server_name  www.51xit.top;    #改域名
        charset utf-8;               #改字符集
        access_log  /usr/local/nginx/logs/www.51xit.top.access.log;  #改日志存储路径 去掉main
         localtion /post {
    
    	
                rewrite (.+) http://www.52xit.top/bbs$1 permanent;
        }
[root@localhost ~]# killall -s HUP nginx   #重启

Enter www.51xit.top/bbs in the real machine browser to jump to www.51xit.top/new/bbs
Insert picture description here
Insert picture description here

2.4. Jump based on parameter matching

Now visit http://www.51xit.top/100-(100|200)-100.html to jump to http://www.51xit.top page

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
    
    
        listen       80;
        server_name  www.51xit.top;    #改域名
        charset utf-8;               #改字符集
        access_log  /usr/local/nginx/logs/www.51xit.top.access.log;  #改日志存储路径 去掉main
   if ($request_uri ~ ^/100-(100|200)-(\d+).html$){
    
    
                rewrite (.*) http://www.51xit.top permanent;
        }     
[root@localhost ~]# killall -s HUP nginx   #重启      

Enter www.51xit.top/100-200-100.html in real machine browser
Insert picture description here

2.5. Jump based on the most common URL request

Visit a specific page and jump to the homepage

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
    
    
        listen       80;
        server_name  www.51xit.top;    #改域名
        charset utf-8;               #改字符集
        access_log  /usr/local/nginx/logs/www.51xit.top.access.log;  #改日志存储路径 去掉main
  location ~* ^/1/test.html {
    
    
                rewrite (.+) http://www.51xit.top permanent;
        }    
 [root@localhost ~]# killall -s HUP nginx   #重启    

Enter http://www.51xit.top/1/test.html in the real machine browser
Insert picture description here

2.6. Jump based on all files ending with php in the directory

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
    
    
        listen       80;
        server_name  www.51xit.top;    #改域名
        charset utf-8;               #改字符集
        access_log  /usr/local/nginx/logs/www.51xit.top.access.log;  #改日志存储路径 去掉main
  location ~* /upload/.*\.php$ {
    
    
                rewrite (.+) http://www.51xit.top permanent;
        }    
 [root@localhost ~]# killall -s HUP nginx   #重启    

Enter http://www.51xit.top/upload/bbs/1.php in real machine browser
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48191211/article/details/109366572