Implementation of rewrite Jump in nginx
- 2021-09-11 21:53:21
- OfStack
1. Jump between old and new domain names
Scenario: Jump based on domain name, now the old domain name of the company: www. peihua. com
There are business requirements to change, and the new domain name www. zhenguo. 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
Configure dns, configure www. peihua. com (old) and www. zhenguo. com (new) parsing, respectively
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
(You must have an official source for yum to install nginx)
yum install nginx -y
rpm -qc nginx // Find configuration files
Modify the configuration file of nginx
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name www.peihua.com; # Domain name modification
#charset koi8-r;
access_log /var/log/nginx/peihua.com-access.log main; # Log modification
location / {
# Domain name redirection
if ($host = 'www.peihua.com') {
rewrite ^/(.*)$ http://www.zhenguo.com/$1 permanent;
}
root /usr/share/nginx/html;
index index.html index.htm;
}
[root@localhost named]# systemctl restart nginx
Configure domain name resolution
yum -y install bind
# Modify the master configuration file
[root@localhost conf.d]# vim /etc/named.conf
options {
listen-on port 53 { any; }; // Modify 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; }; // Modify to any
# Modify the zone profile
[root@localhost conf.d]# vim /etc/named.rfc1912.zones
zone "peihua.com" IN {
type master;
file "peihua.com.zone";
allow-update { none; };
};
zone "zhenguo.com" IN {
type master;
file "zhenguo.com.zone";
allow-update { none; };
};
# Modify the area data file
[root@localhost conf.d]# cd /var/named/
[root@localhost named]# cp -p named.localhost peihua.com.zone
[root@localhost named]# cp -p peihua.com.zone zhenguo.com.zone
[root@localhost named]# systemctl start named
Browser testing
Browser input simulates access to http://www. peihua. com/test/1/index. php (although this request content does not exist) jumps to
http://www.zhneguo.com/test/1/index.php, 301 can be seen from headers, which realizes permanent jump, and the parameters after the domain name also jump normally.
2. IP-based access jump
Action Scenario: Based on the client IP access jump, for example, the company business version is launched today, all IP access content will display a fixed maintenance page, only the company IP: 12.0. 0.100 access is normal
Modify the nginx configuration file
[root@localhost html]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name www.peihua.com;
#charset koi8-r;
charset 'utf-8'; // Recognize Chinese characters
access_log /var/log/nginx/peihua.com-access.log main;
# Is the setting legal IP Sign
set $rewrite ture;
# Judge whether it is legal or not IP
if ($remote_addr = "12.0.0.100") {
set $rewrite false;
}
# Illegal IP Make a judgment and mark it
if ($rewrite = ture) {
rewrite (.+) /maintenance.html;
}
# Match the tag to jump to the site
location = /maintenance.html {
root /usr/share/nginx/html;
}
Add custom page content to maintenance. html
[root@localhost html]# cat /usr/share/nginx/html/maintenance.html
<h1> Website under maintenance </h1>
Browser testing
Use ip address 12.0. 0.100 to access http://www.peihua.com, which can be accessed normally
Use IP address 12.0. 0.99 to access http://www.peihua.com to display the maintenance page
3. Jump to the new domain name based on the old domain name and add a directory
Scenario: Based on the old domain name, jump to the new domain name and add a directory. For example, you are now accessing http://bbs.peihua.com.
Now you need to jump all the posts under this domain name to http://www.peihua.com/bbs, and pay attention to keeping the parameters unchanged after the domain name jump
Modify the nginx configuration file
[root@localhost named]# vim /etc/nginx/conf.d/default.conf
listen 80;
server_name bbs.peihua.com;
#charset koi8-r;
charset 'utf-8';
access_log /var/log/nginx/peihua.com-access.log main;
location /post {
rewrite (.+) http://www.peihua.com/bbs$1 permanent;
}
Note: accp. com. zone needs to change the host domain name resolution from www to bbs
[root@localhost named]# cd /var/named
[root@localhost named]# vim peihua.com.zone
$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 12.0.0.25
[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx
Browser access
The browser accesses http://bbs.peihua.com/post/a.html, which is called www.peihua.com/bbs/post/a.txt
4. Jump based on parameter matching
Action scenario: Jump based on parameter matching, for example, now visit http://www. peihua. com/100-(100 200)-100. html
Jump to the http://www.peihua.com page
Modify the nginx configuration file
[root@localhost named]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name www.peihua.com;
#charset koi8-r;
charset 'utf-8';
access_log /var/log/nginx/peihua.com-access.log main;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.*) http://www.peihua.com permanent;
}
Note:\ d matches 1 number, between 0 and 9
Modify dns and change bbs to www
[root@localhost named]# vim peihua.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 12.0.0.25
[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx
Browser testing
The browser accesses http://www.peihua.com/100-200-26. html, which is called the homepage of www.peihua.com
5. Based on all jumps ending in php and based on a certain 1 page
Modify the nginx configuration file
yum install nginx -y
rpm -qc nginx // Find configuration files
0
Browser access
The browser accesses http://www.peihua.com/upload/a.php, which is called www.peihua.com by jump
6. Jump based on a specific 1 page/abc/123. html
Modify the nginx configuration file
yum install nginx -y
rpm -qc nginx // Find configuration files
1
Browser access
The browser accesses http://www.peihua.com/abc/123. html, which is called www.peihua.com