Nginx - rewrite (application scenario example)

Keywords: Linux Nginx vim PHP RPM

Scenario 1 - Domain Name Based Jump

The company's old domain name, www.accp.com, needs to be replaced by the new domain name, www.kgc.com, due to changes in business needs
    Old domain names cannot be abolished
    Jump from the old domain name to the new domain name with its parameters unchanged

Experimental environment

Linux Server (192.168.13.144)
Tester win7

1, Install the Nginx service

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
##Install nginx official source
//Warning: /var/tmp/rpm-tmp.vS0k20: Header V4 RSA/SHA1 Signature, Key ID 7bd9bf62: NOKEY
//Preparing...
//Upgrading/Installing...
     1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@localhost ~]# yum install nginx -y   ##yum install nginx

2, Modify nginx default profile

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf  ##Modify Default Profile
server {
        listen       80;
        server_name  www.accp.com;   ##Modify Host Name

        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;   ##Open Logging Service

3. Install the bind resolution service

[root@localhost ~]# yum install bind -y

4, modify the main configuration file (named.conf)

[root@localhost ~]# vim /etc/named.conf 
options {
                                listen-on port 53 { any; };          ##Listen on all
                                listen-on-v6 port 53 { ::1; };
                                directory       "/var/named";
                                dump-file       "/var/named/data/cache_dump.db";
                                statistics-file "/var/named/data/named_stats.txt";
                                memstatistics-file "/var/named/data/named_mem_stats.txt";
                                recursing-file  "/var/named/data/named.recursing";
                                secroots-file   "/var/named/data/named.secroots";
                                allow-query     { any; };           ##Allow all

5, Modify Zone Profile (named.rfc1912.zones)

[root@localhost ~]# vim /etc/named.rfc1912.zones    ##Configuration Zone Profile

zone "accp.com" IN {
                                type master;
                                file "accp.com.zone";             ##accp zone data profile
                                allow-update { none; };
};

6, Modify the region data profile (accp.com.zone)

[root@localhost ~]# cd /var/named/  
[root@localhost named]# cp -p named.localhost accp.com.zone    ##Copy Template
[root@localhost named]# vim accp.com.zone    ##Modify Zone Profile

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                                                        1D      ; refresh
                                                                        1H      ; retry
                                                                        1W      ; expire
                                                                        3H )    ; minimum
                                NS      @
                                A       127.0.0.1
www IN  A       192.168.13.144     ##Local Address
[root@localhost named]# systemctl start named      ##Open dns Service
[root@localhost named]# systemctl stop firewalld.service    ##Close Firewall
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start nginx   ##Open nginx service
[root@localhost named]# netstat -ntap | grep nginx  ##View Port
tcp   0    0 0.0.0.0:80      0.0.0.0:*       LISTEN   4093/nginx: master 

7, Test Web Page with Test Machine


8, Modify configuration file, set up domain name jump

[root@localhost named]# vim /etc/nginx/conf.d/default.conf  ##Modify Profile
server {
        listen       80;
        server_name  www.accp.com;

        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;

        location / {
                if ($host = "www.accp.com"){        ##Match If Domain Name is Old Domain Name
                                rewrite ^/(.*)$ http://www.kgc.com/ permanent; ## permanently sets the jump to a new domain name
                }
                root   /usr/share/nginx/html;
                index  index.html index.htm;
        }

9, Add a new domain name resolution

[root@localhost named]# vim /etc/named.rfc1912.zones 

zone "kgc.com" IN {
                                type master;
                                file "kgc.com.zone";             ##accp zone data profile
                                allow-update { none; };
};

[root@localhost named]# cp -p /var/named/accp.com.zone /var/named/kgc.com.zone
##Copy zone data profile as kgc data profile
[root@localhost named]# systemctl restart named   ##Restart Resolution Service
[root@localhost named]# systemctl restart nginx     ##Restart nginx service

10, access with old domain name, see jumps


11. Add parameters to the old domain name to see if there are any parameters when jumping to a new domain name


Scenario 2 - Client-based IP Access Jump

Company Business Version is online, all IP access to anything shows a fixed maintenance page, only company IP access is normal

1, Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf 

server {
        listen       80;
        server_name  www.accp.com;
        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;
        #Set legal IP flag
        set $rewrite true;         ##Set variable to true
        #Determine whether it is a legitimate IP
        if ($remote_addr = "192.168.13.140"){
                set $rewrite false;    ##Match legitimate IP, set variable to false, normal page Jump
        }
        #Label illegal IP for judgment
        if ($rewrite = true){                ##Match illegal IP, jump to main's page
                rewrite (.+) /main.html;
        }
        #Match tags to jump sites
        location = /main.html {              ##Exact match
                root /usr/share/nginx/html;   ##Site Path
        }

        location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm;
        }  

2, Create illegal IP sites and main pages

[root@localhost conf.d]# cd /usr/share/nginx/html/  ##Switch to Site
[root@localhost html]# vim main.html    ##Edit illegal IP access page content
<h1>this is test web</h1>
[root@localhost html]# systemctl restart nginx   ##Restart the Nginx service

3, Visit Web Pages


Scenario 3 - New domain name jump and catalog based on old

Jump all posts below the domain name http://bbs.accp.com to http://www.accp.com/bbs and leave the parameters unchanged after the domain name jumps

1, Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf   ##Modify Default Profile
server {
        listen       80;
        server_name  bbs.accp.com;   ##Modify service name

        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;
        location /post {          ##Matching post directory with location
                rewrite (.+) http://www.accp.com/bbs permanent; ##Permanent redirect jump
        }

2, modify dns region data profile (accp.com.zone)

[root@localhost conf.d]# cd /var/named/
[root@localhost named]# vim accp.com.zone   ##Modify Zone Data Profile
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                                    0       ; serial
                                                    1D      ; refresh
                                                    1H      ; retry
                                                    1W      ; expire
                                                    3H )    ; minimum
                NS      @
                A       127.0.0.1
bbs IN  A       192.168.13.144
[root@localhost named]# systemctl restart named   ##Restart Resolution Service
[root@localhost named]# systemctl restart nginx     ##Restart the Nginx service
[root@localhost named]# echo "nameserver 192.168.13.144" > /etc/resolv.conf 
##Put Resolution Server Address in Local Resolution Profile

3, Test Web Page


Scenario 4 - Jump based on parameter matching

Browser Access
 http://www.accp.com/100-(100|200)-100.html Jump to http://www.accp.com page

1, Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf 

server {
        listen       80;
        server_name  www.accp.com;
        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;
        if ($request_uri ~ ^/100-(100|200)-(\d+).html$){       
        ##Match regular begins with 100 - (100|200) - integer html that ends multiple times
                rewrite (.*) http://www.accp.com permanent; ##Permanent redirection jumps to home page
        }

2, Modify dns zone data profile

[root@localhost conf.d]# vim /var/named/accp.com.zone  ##Modify Zone Data Profile
www IN  A       192.168.13.144   
[root@localhost conf.d]# systemctl restart named  ##Restart Resolution Service 
[root@localhost conf.d]# systemctl restart nginx     ##Restart the Nginx service

3, Test Web Page


Scenario 5 - Jump based on all PHP files in the directory

Go to http://www.accp.com/upload/1.php to jump to the first page

1, Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf    ##Modify Default Profile
server {
        listen       80;
        server_name  www.accp.com;
        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;
        location ~* /upload/.*\.php$ {         ##Matches are case-insensitive, zero or more times ending in.php after upload matches
                rewrite (.+) http://www.accp.com permanent; ##Jump to Home Page
        }
[root@localhost conf.d]# systemctl restart nginx   ##Restart the Nginx service

2, Test Web Page


Scenario 6 - Jump based on the most common url request

Visit a specific page and jump to the first page

1, Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf    ##Modify Nginx default profile
server {
        listen       80;
        server_name  www.accp.com;
        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;
        location ~* ^/abc/123.html {       ##Match a particular page
                rewrite (.+) http://www.accp.com permanent; ##Jump to Home Page
        }
[root@localhost conf.d]# systemctl restart nginx   ##Restart the Nginx service

2, Test Web Page


Thank you for reading!

Posted by redking on Mon, 18 Nov 2019 12:35:23 -0800