Nginx Rewrite configuration

One, Rewrite overview

1. The role of
Rewrite rules Rewrite, also known as address redirection, can rewrite and redirect URLs

2.Rewrite jump scene

  • URL looks more standardized and reasonable
  • Enterprises will disguise dynamic URL addresses as static addresses to provide services
  • After the URL is changed to a new domain name, let the old visits jump to the new domain name
  • Some business adjustments on the server

3.Rewrite actual scene

3.1 How to implement Nginx jump requirements

  • Use rewrite for matching jump
  • Jump after matching global variables using if
  • Use location matching to jump

3.2 Rewrite is placed in the server{}, if{}, location{} section (more important, need to be kept in mind)

  • location only works on the string after the domain name, except for the passed parameters

3.3 To the domain name or parameter string

  • Use if global variable matching
  • Use proxy_pass reverse proxy

Two, Nginx regular expression

1. Commonly used regular expression metacharacters

Insert picture description here

2. Commonly used expressions for regular matching

Insert picture description here

Three, Rewrite command

1. Command syntax

 rewrite       <regex>       <replacement>           [flag]
              正则            跳转后的内容              rewrite支持的flag标记

Flag description
Insert picture description here

2. Last and break comparison

Insert picture description here

Four, location overview

1.location matching

location = patt {
    
    }  [精确匹配]
location patt{
    
    }      [一般匹配]
location  ~patt{
    
    }   [正则匹配]

2.location priority

For expressions of the same type, strings of strings will be matched
first in order of priority

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

Note: The order of execution is from top to bottom

3.location priority rules

3.1 Similarities

  • Can jump

3.2 Differences

  • Rewrite is to change the path to obtain resources within the same domain name
  • Location is to control access or reverse proxy for a type of path, and it can also proxy_pass to other machines

3.3 rewrite will unload the location, execution order

  • Execute the rewrite instruction in the server block
  • Execute locationpipei
  • Execute the rewrite command in the selected location

Five, application examples

Note: Remember to initialize the Nginx environment after each function to avoid conflicts between the previous function and the subsequent configuration file

1. Redirect based on domain name

Now the company's old domain name www.fa.com has business requirements and changes, and the new domain name www.newfa.com needs to be used instead.

First go to the hosts file in C:\Windows\System32\drivers\etc of this machine to add the IP and new domain name.
Note: If you can’t save it, save it on the desktop, delete the text in the file, and copy the file on the desktop. Remember to remove the .txt suffix of hosts, and then restart the service

1. Modify the default site location

vi /etc/nginx.conf
nginx  -t    #检查语法

添加:
server {
    
    
        listen       80;
        server_name  localhost;
        if ($host = 'www.fa.com')
        {
    
    
            rewrite ^/(.*)$ http://www.newfa.com/$1 permanent;
        }

Insert picture description here
2. Add the site location of the new domain name www.netfa.com (add at the end)

vi /usr/local/nginx/conf/nginx.conf
nginx   -t

添加:
server {
    
    
        listen 80;
        server_name www.newfa.com;
        charset utf-8;
        access_log /var/log/nginx/www.newfa.com-access.log main;
        location / {
    
    
            root /usr/share/nginx/html;
            index index.html index.htm;
        }
    }
[root@server1 ~]mkdir /var/log/nginx
[root@server1 ~]mkdir -p /usr/share/nginx/html
[root@server1 ~]echo "Hello Fa" > /usr/share/nginx/html/index.html
[root@server1 ~]# systemctl restart nginx

Test:
www.netfa.com
Insert picture description here

2. Access based on client IP

All other IPs access the company’s website and display the maintenance page. Only the company’s IP access is normal.
Assume that the server IP is 20.0.0.14 and the company IP is 20.0.0.13.

1. Modify the configuration file, add redirection

[root@server1 ~]# vi /etc/nginx.conf

添加:
server {
    
    
        listen       80;
        server_name  www.fa.com;
        set $rewrite true;                  ###判断标志$rewrite
        if ($remote_addr = "20.0.0.13") {
    
           #允许公司内部访问,更改标志位false(这样不会执行下面if判断的rewrite了)
              set $rewrite false;
              }
        if ($rewrite = true) {
    
    
              rewrite (.+) /newfa.html;            ##如果不是公司IP,则重定向到维护页面
              }
        charset utf-8;

        access_log  logs/old.com.access.log  main;

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

Insert picture description here
2. Check grammar and create virtual ip

[root@server1 ~]# vi /etc/hosts
20.0.0.13       www.fa.com   www.newfa.com        
[root@server1 ~]# nginx -t
[root@server1 ~]# ifconfig ens33:1  20.0.0.19
[root@server1 ~]# ifconfig
[root@server1 ~]# systemctl restart nginx

Insert picture description here
3. Add a maintenance page

[root@server ~]# vi /usr/local/nginx/html/newfa.html
Website is Maintaining,Please visit later!!!  维护中

Test:
Use the 20.0.0.13 host to visit www.old.com, you can visit the original web page
Insert picture description here

Use other hosts to access directly will prompt maintenanceInsert picture description here

3. Jump and add directories based on the old and new domain names

Redirect all posts under the domain name www.fa.com to www.newfa.com/fa, and keep the parameters unchanged after the domain name is redirected

1. Add redirect

[root@server1 ~]# vi /etc/nginx.conf 
[root@server1 ~]# nginx -t

Insert picture description here

2. Add a new domain name at the end

[root@server1 ~]# vi /etc/nginx.conf 
[root@server1 ~]# nginx -t

Insert picture description here
3. Create a fa page in the server

[root@server1 html]# vi /etc/nginx.conf 
[root@server1 html]# systemctl restart nginx
[root@server1 html]# cd
[root@server1 ~]# cd /usr/local/nginx/html/
[root@server1 html]# mkdir -p fa/post
[root@server1 html]# cd fa/post
[root@server1 post]# vi 1.html
[root@server1 post]# cat 1.html
添加:
Hello  work!
[root@server1 post]# cat /etc/hosts
添加:
20.0.0.13  www.fa.com  www.newfa.com

Test:
http://www.fa.com/post/1.html will automatically jump to the new page of www.newfa.com
Insert picture description here

4. Jump based on parameter matching

1. Modify the configuration file

[root@server ~]# vi /etc/nginx.conf

添加:
server {
    
    
        listen       80;
        server_name  www.newfa.com;

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

        charset utf-8;

        access_log  logs/newfa.com.access.log  main;

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

2. Check the grammar

[root@server ~]# vi /etc/hosts
20.0.0.13       www.newfa.com       
[root@server ~]# nginx -t
[root@server ~]# systemctl restart nginx

Test
Visit www.newfa.com/100-100-10.html, it will jump to www.newfa.com to
Insert picture description here
visit www.newfa.com/100-300-10.html, and it will not jump to www.newfa if the matching range is exceeded. com
Insert picture description here

5. Jump based on all php files in the directory

Visit http://www.newfa.com/upload/1.php to jump to the homepage

1. Modify the configuration file

[root@server ~]# vi /etc/nginx.conf

添加:
server {
    
    
        listen       80;
        server_name  www.newfa.com;

        location ~* /upload/.*\.php$ {
    
    
             rewrite (.+) http://www.newfa.com permanent;
        }

        charset utf-8;

        access_log  logs/newfa.com.access.log  main;

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

Insert picture description here
2. Check the grammar

[root@server ~]# vi /etc/hosts
20.0.0.13       www.newfa.com      
[root@server ~]# nginx -t
[root@server ~]# systemctl restart nginx

Test:
Visit http://www.newfa.com/upload/1.php, it will jump to www.newfa.com
Insert picture description here

6. Jump based on the most common URL request

Visit a specific page to jump to the homepage, such as browser visit http://www.newfa.com/1/newfa.html to jump to the homepage

1. Modify the configuration file

[root@server ~]# vi /etc/nginx.conf

添加:
server {
    
    
        listen       80;
        server_name  www.newfa.com;

        location ~* ^/1/test.html {
    
    
           rewrite (.+) http://www.newfa.com permanent;
        }

        charset utf-8;

        access_log  logs/newfa.com.access.log  main;

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

Insert picture description here
2. Check the syntax and restart the service

[root@server ~]# vi /etc/hosts
20.0.0.13       www.newfa.com       
[root@server ~]# nginx -t
[root@server ~]# systemctl restart nginx

Test:
Visit http://www.newfa.com/1/newfa.html, it will jump to www.newfa.com
Insert picture description here

Guess you like

Origin blog.csdn.net/F2001523/article/details/110462139