Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of Nginx rewrite Jump Application scenario

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Application scenario 1Mui-Domain name-based Jump

The company's old domain name www.accp.com needs to be replaced by a new domain name www.kgc.com due to changes in business requirements.

The old domain name cannot be abolished.

Jump from the old domain name to the new domain name and keep its parameters unchanged

Experimental environment

Linux Server (192.168.13.144)

Test machine 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## installation nginx official source warning: / var/tmp/rpm-tmp.vS0k20: header V4 RSA/SHA1 Signature Key ID 7bd9bf62: NOKEY in preparation. # # [100%] upgrading / installing... 1:nginx-release-centos-7-0.el7.ngx # # [100 %] [root@localhost ~] # yum install nginx-y # # yum install nginx

2. Modify nginx default 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 the hostname # charset koi8-r; access_log / var/log/nginx/www.accp.com-access.log main; # # enable logging service

3. Install bind parsing 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;}; # # listening 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 the zone configuration file (named.rfc1912.zones)

[root@localhost ~] # vim / etc/named.rfc1912.zones # # configure zone profile zone "accp.com" IN {type master; file "accp.com.zone"; # # accp zone data profile allow-update {none;};}

6. Modify the zone data configuration file (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 the zone configuration file $TTL 1D @ IN SOA @ rname.invalid. (1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.13.144 # # Native address [root@localhost named] # systemctl start named # # enable dns service [root@localhost named] # systemctl stop firewalld.service # # turn off firewall [root@localhost named] # setenforce 0 [root@localhost named] # systemctl start nginx # # enable nginx service [root@localhost named] # netstat-ntap | | grep nginx # # View port tcp 0 0 0.0 0. 0. 0. 0. LISTEN 4093/nginx 80 0. 0. 0. 0. * LISTEN 4093/nginx: master |

7, use the testing 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 the 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 / {if ($host = "www.accp.com") {# # match if the domain name is the old domain name rewrite ^ / (. *) $http://www.kgc.com/$1 permanent; # # then permanently set to jump to the new domain name} root / usr/share/nginx/html; index index.html index.htm;}

9, add new domain name resolution

[root@localhost named] # vim / etc/named.rfc1912.zones zone "kgc.com" IN {type master; file "kgc.com.zone"; # # accp area data configuration file allow-update {none;};} [root@localhost named] # cp-p / var/named/accp.com.zone / var/named/kgc.com.zone## replication zone data configuration file is kgc data configuration file [root@localhost named] # systemctl restart named # # restart resolution service [root@localhost named] # systemctl restart nginx # # restart nginx service

10. Use the old domain name to access and view the jump.

11. Add parameters to the old domain name to see if there are parameters when you jump to the new domain name

Application scenario 2Mel-access redirection based on client IP

The company's business version is online, and all IP visits and any content displays a fixed maintenance page. Only the company's 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 the legal IP flag set $rewrite true # # set the variable to true # determine whether it is a legitimate IP if ($remote_addr = "192.168.13.140") {set $rewrite false; # # match legal IP, set the variable to false, normal jump page} # illegal IP to judge and mark if ($rewrite = true) {# # match illegal IP, jump to main's web page rewrite (. +) / main.html Location = / main.html {# # exactly 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 web pages of main

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

3. Visit the web page

Application scenario 3Mui-based on old and new domain names to jump and add directories

Redirect all posts under the domain name http://bbs.accp.com to http://www.accp.com/bbs and keep 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 the default profile server {listen 80; server_name bbs.accp.com; # # modify the 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$1 permanent; # # permanent redirect}

2. Modify the zone 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.1bbs IN A 192.168.13.144 [root@localhost named] # systemctl restart named # # restart resolution service [root@localhost named] # systemctl restart nginx # # restart Nginx service [root@localhost named] # echo "nameserver 192.168.13.144" > / etc/resolv.conf # # put the resolution server address in the local resolution configuration file

3. Test the web page

Application scenario 4muri-Jump based on Parameter matching

The browser visits http://www.accp.com/100-(100|200)-100.html and jumps to the 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 beginning with 100-(100 | 200)-multiple integer html ending with rewrite (. *) http://www.accp.com permanent; # # permanently redirect to home page}

2. Modify dns zone data configuration file

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

3. Test the web page

Application scenario 5Mel-jump based on all PHP files under the directory

Visit http://www.accp.com/upload/1.php to jump to the home page

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 www.accp.com; # charset koi8-r; access_log / var/log/nginx/www.accp.com-access.log main Location ~ * / upload/.*\ .php ${# # matches the rewrite (. +) http://www.accp.com permanent; # # that ends with .php zero or more times after matching upload regardless of case. Jump to the home page} [root@localhost conf.d] # systemctl restart nginx # # restart the Nginx service

2, test the web page

Application scenario 6Mel-Jump based on the most common url request

Visit a specific page and jump to the home 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 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 {# # matches a specific web page rewrite (. +) http://www.accp.com permanent; # # Jump to the home page} [root@localhost conf.d] # systemctl restart nginx # # restart the Nginx service

2, test the web page

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report