Nginx -- rewrite application example

Application example 1. Domain name based jump

Now the old domain name www.old.com of the company needs to be replaced by the new domain name www.new.com, but the old domain name can not be abolished, and it needs to jump to the new domain name, and the later parameters remain unchanged.

1. Install 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
 In the preparation, we should be more serious in the party and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people and the people[100%]
Upgrading / installing
     1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root @ localhost ~] (Yum install nginx - Y / / Yum install nginx

2. Modify the Nginx configuration file

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf / / modify the default configuration file
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;              //Turn on log service

3. Install DNS domain name resolution service

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

4. Modify the main configuration file

[root@localhost ~]# vim /etc/named.conf 
options {
                                listen-on port 53 { any; };                        //Listen to all addresses
                                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 the regional configuration file

[root@localhost ~]# vim /etc/named.rfc1912.zones / / configure the zone configuration file

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

6. Modify regional data configuration file

[root@localhost ~]# cd /var/named/  
[root@localhost named]# cp -p named.localhost accp.com.zone
//Copy zone data profile template
[root@localhost named]# vim accp.com.zone / / modify the zone configuration file

$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.131.133              //Local address [root @ localhost named] ා systemctl start named / / start dns Service
[root@localhost named]# systemctl stop firewalld.service / / turn off the firewall
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start nginx / / start nginx service
[root@localhost named]# netstat -ntap | grep nginx / / view the port
tcp   0    0 0.0.0.0:80      0.0.0.0:*       LISTEN   4093/nginx: master 

7. Set the dns resolution address of the test machine as the virtual machine address, and use the test machine to test the web page

8. Modify the configuration file and set the domain name jump

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

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

        location / {
                if ($host = "www.old.com"){               //Match if it is an old domain name
                                rewrite ^/(.*)$ http://www.new.com/ permanent; / / to permanently set the jump to the new domain name
                }
                root   /usr/share/nginx/html;
                index  index.html index.htm;
        }

9. Add new domain name resolution in DNS configuration file

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

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

[root@localhost named]# cp -p /var/named/accp.com.zone /var/named/kgc.com.zone
//Copy the original regional data profile to the regional data profile of kgc.com
[root@localhost named]# systemctl restart named / / restart dns resolution service
[root@localhost named]# systemctl restart nginx / / restart nginx service

10. Visit with the old domain name to check whether the web page jumps to the new domain name

11. Add parameters after the old domain name to check whether there are still parameters when jumping to the new domain name

Application example 2. IP access jump based on client

For example, when the company's business version goes online today, a fixed maintenance page will be displayed for all IP accesses. Only the company's IP: 192.168.131.128 accesses normally.

1. Modify the 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 whether to allow access to the IP flag
        set $rewrite true;                  //Set variable to true
        //Determine whether it is the IP allowed to access
        if ($remote_addr = "192.168.131.128"){
                set $rewrite false;            //Match the legal IP, set the variable to false, and jump to the page normally
        }
        //The IP that is not allowed to be accessed is marked for judgment
        if ($rewrite = true){                       //Match illegal IP, jump to maintenance page
                rewrite (.+) /main.html;
        }
        #Match tags to jump to site
        location = /main.html {                      //Exact matching
                root /usr/share/nginx/html;        //Site path
        }

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

2. Create IP sites that are not allowed to visit and maintain web pages

[root@localhost conf.d]# cd /usr/share/nginx/html / / / switch to the site
[root@localhost html]# vim main.html / / edit the content of the illegal IP page
<h1>this is test web</h1>
[root@localhost html]# systemctl restart nginx / / restart Nginx service

3. Use the pop-up page when IP access is not allowed

4. Pages that can be accessed during legal IP access

Application example 3. Jump to the new domain name and add directory based on the old domain name

For example, what I'm visiting now is http://bbs.accp.com , now you need to jump to http://www.accp.com/bbs , note to keep the parameters after domain name jump unchanged.

1. Modify the Nginx default configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf / / modify the default configuration file
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 {                          //Match post directory with location
                rewrite (.+) http://www.accp.com/bbs permanent; / / redirect jump permanently
        }

2. Modify the region data configuration file accp.com.zone of dns

[root@localhost conf.d]# cd /var/named/
[root@localhost named]# vim accp.com.zone / / modify the zone data configuration file
$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.131.133
[root@localhost named]# systemctl restart named / / restart dns domain name resolution service
[root@localhost named]# systemctl restart nginx / / restart Nginx service
[root@localhost named]# echo "nameserver 192.168.13.144" > /etc/resolv.conf 
//Place the resolution server address in the local resolution configuration file

Entering the post directory and parameters will jump to bbs and the parameters after it. Because of the version problem of the browser, the parameters behind the domain name cannot be displayed here

Application example 4. Jump based on parameter matching

For example, visit now http://www.accp.com/100-(100|200)-100.html Jump to http://www.accp.com Page.

1. Modify the 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 the integer html whose regular start is 100 - (100| 200) - multiple times at a time
                rewrite (.*) http://www.accp.com permanent; redirect to home page permanently
        }

2. Modify the region data configuration file accp.com.zone of dns

[root@localhost conf.d]# vim /var/named/accp.com.zone / / modify the zone data configuration file
www IN  A       192.168.131.133   
[root@localhost conf.d]# systemctl restart named / / restart dns domain name resolution service 
[root@localhost conf.d]# systemctl restart nginx / / restart Nginx service

Enter 100-100-99.html to jump to accp Homepage

Application example 5. Jump based on all php ending files in the directory

Visit http://www.accp.com/upload/1.php Jump to home

Modify Nginx default profile

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf / / modify the default configuration file
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$ {                                                                                      //Match is not case sensitive. Match zero or more times after upload with. php as the end
                rewrite (.+) http://www.accp.com permanent; / / go to the homepage
        }
[root@localhost conf.d]# systemctl restart nginx / / restart Nginx service

Visit the page / upload / ending in. php to jump to the accp Homepage

Application example 6. Jump based on the most common url request

Visit a specific page and jump to the homepage

Modify Nginx default profile

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf / / modify the Nginx default configuration file
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 specific web page
                rewrite (.+) http://www.accp.com permanent; / / go to the homepage
        }
[root@localhost conf.d]# systemctl restart nginx / / restart Nginx service

Visit a specific web page to jump back to the accp home page

Keywords: Linux Nginx vim DNS PHP

Added by Steffen on Wed, 27 Nov 2019 07:23:18 +0200