Application instance of rewrite Module of Nginx service (actual combat! Can do with it!)

Experimental environment Linux CentOS7 virtual machine (IP: 192.168.52.132)win10 virtual machine (as test machine) 1. Install Nginx and bind services...
Experimental environment

Linux CentOS7 virtual machine (IP: 192.168.52.132)
win10 virtual machine (as test machine)

1. Install Nginx and bind services

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm //Get http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm //Warning / var/tmp/rpm-tmp.PtKluI: header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY //[100%] //Upgrading / installing 1:nginx-release-centos-7-0.el7.ngx ################################# [100%] [root@localhost ~]# yum install nginx bind -y ..........//Omit installation process

2. Modify the main configuration file named.conf

[root@localhost sbin]# vim /etc/named.conf options { listen-on port 53 { any; }; //127.0.0.1 to any 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; }; //Change localhost to any [root@localhost sbin]#

3. Modify the region configuration file named.rfc1912.zones

[root@localhost sbin]# vim /etc/named.rfc1912.zones zone "old.com" IN { //Add a zone information type master; file "old.com.zone"; allow-update { none; }; }; [root@localhost sbin]#

4 / modify area data profile

[root@localhost sbin]# cd /var/named/ [root@localhost named]# ls data dynamic named.ca named.empty named.localhost named.loopback slaves [root@localhost named]# cp -p named.localhost old.com.zone / / copy the template and name it [root@localhost named]# vim old.com.zone $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.52.132 //Add resolution address [root@localhost named]#

5. Enable dns service and Nginx service

[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 / / turn off enhanced security [root@localhost named]# systemctl start nginx / / start Nginx service [root@localhost named]# netstat -ntap | grep 80 / / view port tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 51197/nginx: master [root@localhost named]#

6. Set the dns address of win10 host to 192.168.52.132

7. Use win10 host to test whether it can be resolved (the resolution is successful)

Application example 1 -- jump based on domain name

Now the company's old domain name www.old.com has business needs to be changed;
The new domain name www.new.com needs to be used instead, but the old domain name cannot be abolished;
You need to jump to the new domain name, and the following parameters remain unchanged.

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

[root@localhost named]# cd /etc/nginx/ [root@localhost nginx]# vim 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 the domain name is an old domain name rewrite ^/(.*)$ http://www.new.com/ permanent; ා the new domain name will be set permanently } root /usr/share/nginx/html; index index.html index.htm; }

2. Add new domain name resolution

[root@localhost nginx]# vim /etc/named.rfc1912.zones zone "new.com" IN { type master; file "new.com.zone"; #new area data profile allow-update { none; }; }; [root@localhost nginx]# cd /var/named/ [root@localhost named]# ls data dynamic named.ca named.empty named.localhost named.loopback old.com.zone slaves [root@localhost named]# cp -p old.com.zone new.com.zone #Copy the data profile with area data profile as kgc [root@localhost named]# systemctl restart named #Restart the resolution service [root@localhost named]# systemctl restart nginx #Restart nginx service [root@localhost named]#

3. Visit with the old domain name and view the jump


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


Application example 2 -- skip based on client IP access

All IP accesses display a fixed maintenance page for any content, and only the company IP: 192.168.52.129 accesses normally.

1. View the IP address of win10 host

2. Modify the Nginx default configuration file

[root@localhost named]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; #Set legal IP flag set $rewrite true; #Judge whether it is a legal IP if ($remote_addr = "192.168.52.128"){ #The ip of win10 host is 192.168.52.129, and 129 is illegal ip set $rewrite false; } #Illegal IP is judged and marked if ($rewrite = true){ rewrite (.+) /main.html; } #Match tags to jump to site location = /main.html { root /usr/share/nginx/html; } location / { root /usr/share/nginx/html; index index.html index.htm; }

3. Create illegal IP site and main web page

[root@localhost named]# cd /usr/share/nginx/html/ #Switch to site [root@localhost html]# ls 50x.html index.html [root@localhost html]# vim main.html #Edit illegal IP access web content <h1>this is error page</h1> [root@localhost html]# systemctl restart nginx #Restart Nginx service [root@localhost html]#

4. Win10 host accesses the web page (win10 ip is illegal ip at this time)

5. Modify the Nginx default configuration file

[root@localhost html]# vim /etc/nginx/conf.d/default.conf #Judge whether it is a legal IP if ($remote_addr = "192.168.52.129"){ #Change legal IP to win10 host IP address set $rewrite false; } [root@localhost html]# systemctl restart nginx #Restart service [root@localhost html]#

6. Use win10 host again to visit the web page

Application example 3: skip 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, please keep the parameters after domain name jump unchanged.

1. Modify the Nginx default configuration file

[root@localhost html]# vim /etc/nginx/conf.d/default.conf #Modify default profile server { listen 80; server_name bbs.old.com; #Modify service name #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location /post { #Match post directory with location rewrite (.+) http://www.old.com/bbs permanent } location / { root /usr/share/nginx/html; index index.html index.htm; }

2. Modify the dns regional data configuration file (old.com.zone)

[root@localhost html]# cd /var/named/ [root@localhost named]# ls data dynamic named.ca named.empty named.localhost named.loopback new.com.zone old.com.zone slaves [root@localhost named]# vim old.com.zone #Modify area 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.52.132 #www changed to bbs [root@localhost named]# systemctl restart named #Restart the resolution service [root@localhost named]# systemctl restart nginx #Restart Nginx service [root@localhost named]#

3. Test the web page with win10 host


Application example 4 -- jump based on parameter matching

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 named]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.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.old.com permanent; redirect to home page permanently }

2. Modify dns area data configuration file

[root@localhost named]# vim /var/named/old.com.zone #Modify area data profile $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.52.132 [root@localhost named]# systemctl restart named #Restart the resolution service [root@localhost named]# systemctl restart nginx #Restart the resolution service [root@localhost named]#

3. Test the web page with win10 host


Application example 5 -- jump based on all php ending files in the directory

1. Modify the Nginx default configuration file

[root@localhost named]# vim /etc/nginx/conf.d/default.conf #Modify default profile server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.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.old.com permanent; jump to home page } [root@localhost named]# systemctl restart nginx #Restart Nginx service [root@localhost named]#

2. Test the web page with win10 host


Application example 6: jump to home page based on the most common url request

1. Modify the Nginx default configuration file

[root@localhost named]# vim /etc/nginx/conf.d/default.conf #Modify Nginx default profile server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location ~* ^/abc/123.html { #Match a specific web page rewrite (.+) http://www.old.com permanent; jump to home page } location / { root /usr/share/nginx/html; index index.html index.htm; } [root@localhost named]# systemctl restart nginx #Restart Nginx service [root@localhost named]#

2. Test the web page with win10 host


2 December 2019, 21:57 | Views: 6460

Add new comment

For adding a comment, please log in
or create account

0 comments