小知识:nginx里的rewrite跳转的实现

一. 新旧域名跳转

作用场景:基于域名的跳转,现在公司旧域名:www.peihua.com

有业务需求要变更,需要使用新域名www.zhenguo.com代替,但是旧域名不能废除。需要跳转到新域名上,而且后面的参数保持不变

配置dns,分别配置www.peihua.com(old)和www.zhenguo.com(new)解析

?
1
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

(必须要有官方源才能yum安装nginx)

?
1
2
yum install nginx -y
rpm -qc nginx    //查找配置文件

修改nginx的配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
listen    80;
server_name www.peihua.com;                            #域名修改
#charset koi8-r;
access_log /var/log/nginx/peihua.com-access.log main;        #日志修改
location / {
#域名重定向
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

配置域名解析

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
yum -y install bind
#修改主配置文件
[root@localhost conf.d]# vim /etc/named.conf
options {
listen-on port 53 { any; };      //修改成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; };       //修改成any
#修改区域配置文件
[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; };
};
#修改区域数据文件
[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

浏览器测试

浏览器输入模拟访问 http://www.peihua.com/test/1/index.php(虽然这个请求内容) 是不存在的)跳转到

http://www.zhneguo.com/test/1/index.php,从headers 里面 可以看到301,实现了永久跳转,而且域名后的参数也正常跳转。

二. 基于IP访问跳转

作用场景:基于客户端IP访问跳转,例如今天公司业务版本上线,所有IP 访问任何内容都显示一个固定维护页面,只有公司IP:12.0.0.100访问正常

修改nginx配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost html]# 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;
#设置是否合法的IP标志
set $rewrite ture;
#判断是否为合法IP
if ($remote_addr = “12.0.0.100”) {
set $rewrite false;
}
#非法IP进行判断打上标记
if ($rewrite = ture) {
rewrite (.+) /maintenance.html;
}
#匹配标记进行跳转站点
location = /maintenance.html {
root /usr/share/nginx/html;
}

给maintenance.html添加自定义页面内容

?
1
2
[root@localhost html]# cat /usr/share/nginx/html/maintenance.html
<h1>网站正在维护</h1>

浏览器测试

使用ip地址为12.0.0.100 进行访问http://www.peihua.com,可以正常访问

使用IP地址为12.0.0.99进行访问http://www.peihua.com,显示维护页面

三. 基于旧域名跳转到新域名后面加目录

作用场景:基于旧域名跳转到新域名后面加目录,例如现在访问的是http://bbs.peihua.com.

现在需要将这个域名下面的发帖都跳转到http://www.peihua.com/bbs,注意保持域名跳转后 的参数不变

修改nginx配置文件

?
1
2
3
4
5
6
7
8
9
10
[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;
}

注意:accp.com.zone 需要更改主机域名解析,把www改成 bbs

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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

浏览器访问

浏览器访问 http://bbs.peihua.com/post/a.html,被跳转称为www.peihua.com/bbs/post/a.txt

四. 基于参数匹配的跳转

作用场景:基于参数匹配的跳转,例如现在访问 http://www.peihua.com/100-(100 | 200)-100.html

跳转到http://www.peihua.com页面

修改nginx配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[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;
}

注意:\d匹配一个数字,0~9之间

修改会dns,把bbs改成www

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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

浏览器测试

浏览器访问 http://www.peihua.com/100-200-26.html,被跳转称为 www.peihua.com首页

五. 基于所有以php结尾及基于某一个页面的跳转

修改nginx配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.peihua.com permanent;
}
[root@localhost named]# systemctl restart nginx

浏览器访问

浏览器访问 http://www.peihua.com/upload/a.php,被跳转称为 www.peihua.com

六. 基于具体的某一个页面进行跳转/abc/123.html

修改nginx配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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;
location ~* ^/abc/123.html {
rewrite (.+) http://www.peihua.com permanent;
}
[root@localhost named]# systemctl restart nginx

浏览器访问

浏览器访问 http://www.peihua.com/abc/123.html,被跳转称为 www.peihua.com

到此这篇关于nginx里的rewrite跳转的实现的文章就介绍到这了,更多相关nginx rewrite跳转内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_48192495/article/details/109569485

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:教你如何修改Linux远程登录欢迎提示信息

2023-3-27 3:27:02

建站知识

小知识:linux中的分号和&,|和||说明与用法

2023-3-27 3:35:21

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索