Nginx Service-- practical Application of rewrite Module
Nginx Service-- rewrite Module Application practice Demo 1: domain name-based Jump
application scenario: the original domain name is about to be unavailable, now a new domain name is used instead
theory result: input the old domain name, automatically jump to the new domain name, and other parameters remain unchanged
DNS direction
[root@localhost ~] # rpm- Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm / / yum Library upgrade get http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm warning: / var/tmp/rpm-tmp.zvmFF2: header V4 RSA/SHA1 Signature Key ID 7bd9bf62: NOKEY in preparation. # # [100%] upgrading / installing... 1:nginx-release-centos-7-0.el7.ngx # # # [root@localhost] # yum install nginx-y # install the nginx service [root@localhost] # yum install bind-y [root@localhost ~] # vim / etc/named.conf # modify options {listen-on port 53 {any as follows }; # listen on all port 53 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 through [root@localhost ~] # vim / etc/named.rfc1912.zones#### to add the following zone "test.com" IN {type master; file "named.test";} [root@localhost ~] # cp-p / var/named/named.localhost / var/named/named.test [root@localhost ~] # vim / var/named/named.test# modify $TTL 1D @ IN SOA @ rname.invalid as follows. (0; serial 1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.142.128 [root@localhost ~] # systemctl start named [root@localhost ~] # systemctl stop firewalld.service [root@localhost ~] # setenforce 0
Nginx direction
[root@localhost ~] # vim / etc/nginx/conf.d/default.conf#### change server {listen 80; server_name www.test.com; # specify domain name charset utf-8; # specify character set access_log / var/log/nginx/test.com-access.log main as follows # specify the location of access log file [root@localhost ~] # systemctl start nginx [root@localhost ~] # netstat-atnp | grep nginx tcp 0 0 0.0 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
At this point, the original domain name can be accessed normally.
In order to meet the requirements of the experiment, a dns region is now added to the new domain name.
[root@localhost ~] # vim / etc/nginx/conf.d/default.conflocation / {# add the following two lines if ($host = 'www.test.com') {rewrite ^ / (.*) $http://www.yun.com/$1 permanent;} [root@localhost ~] # vim / etc/named.rfc1912.zones### after location to add a new dns area zone "yun.com" IN {type master File "named.yun";}; [root@localhost ~] # cp-p / var/named/named.test / var/named/named.yun [root@localhost ~] # systemctl restart named [root@localhost ~] # systemctl restart nginx
The experiment is successful, and the domain name is automatically transferred from the old domain name to the new domain name.
Demo 2: jump based on client-side IP
application scenario: only individual users can access the website normally during maintenance, and other users can only access the maintenance page.
The result of theory: when a client accesses, the IP is compared automatically and classified according to the table.
DNS direction
is the same as Demo 1 and does not need to be changed.
Nginx direction
[root@localhost ~] # vim / etc/nginx/conf.d/default.conf#### add location / {root / usr/share/nginx/html; index index.html index.htm;} set $ip true; # set variable to true if ($remote_addr = "192.168.142.129") {# match set $ip false when the IP address is "192.168.142.129" # variable changed to false} if ($ip = true) {# when matching variable is true, rewrite ^ / (.*) $/ weihu.html; # jumps to the maintenance page} location = / weihu.html {# matches the root / usr/share/nginx/html to the maintenance page # specify web site} [root@localhost ~] # systemctl restart nginx
At this point, depending on the ip address, you will browse to different pages (normal page / maintenance page)
Demo 3: jump and add directories based on old and new domain names
application scenario: redirect all posts under the domain name http://bbs.test.com to http://www.test.com/bbs, and keep the parameters unchanged after the domain name is redirected
[root@localhost ~] # vim / etc/nginx/conf.d/default.conf### is modified as follows, adding location / new {# if ($request_uri ~ * ^ / new) {rewrite /? (. *) http://www.test.com/bbs/$1 permanent;} [root@localhost ~] # systemctl restart nginx under the location section
Demo 4: jumps to the specified page based on parameter matching
application scenario: after entering a domain name, the user mistakenly enters an error page full of numbers, and the application will automatically jump back to the specified page.
The result of Theory: selection based on regular expression
DNS direction
is the same as all above, no changes are required
Nginx direction
[root@localhost ~] # vim / etc/nginx/conf.d/default.conf### is modified as follows to add if ($request_uri ~ * ^ / (\ d*) .html $) under the location section) {# match html file rewrite (. *) http://www.test.com permanent; # match zero word or multi-word jump to the home page of the site} [root@localhost ~] # systemctl restart nginx
At this point, the html page formed by adding a number after the domain name will automatically jump back to the home page. Example: http://www.test.com/123456.html → http://www.test.com/
Demo 5: based on PHP files, specific pages jump back to the home page
Based on PHP file
[root@localhost ~] # vim / etc/nginx/conf.d/default.conf### modify the # # location paragraph and add location ~ * / upload/ (. +)\ .php ${# based on all php files rewrite (. *) http://www.test.com permanent;} [root@localhost ~] # systemctl restart nginx
There is nothing to say. Files with php will automatically jump back to the home page.
Based on specific html pages
[root@localhost ~] # vim / etc/nginx/conf.d/default.conf### modify the # # location paragraph and add location ~ * / test.html$ {# based on the specific html page rewrite (. *) http://www.test.com permanent;} [root@localhost ~] # systemctl restart nginx
As above, visiting a specific web file will jump back to the home page
Based on any html page
[root@localhost ~] # vim / etc/nginx/conf.d/default.conf### modify # # location / add if ($request_uri ~ * ^ / new/ (. +)\ .html $) {# match to any html page rewrite (. *) http://www.test.com permanent; # Jump to the home page} [root@localhost ~] # systemctl restart nginx
At this point, visiting any web page in html format will automatically jump to the home page
Thank you for reading!