基于客户端 IP 访问跳转,例如今天公司业务版本上线,所有IP 访问任何内容都显示一个固定维护页面,只有公司 IP :192.168.35.130访问正常。
此片基于上一篇《基于域名跳转》进行修改操作
1.修改主配置文件,添加以下内容
[root@localhost named]# vim /etc/nginx/conf.d/default.conf
7 #设置是否合法的IP标志
8 set $rewrite true;
9 #判断是否为合法IP
10 if ($remote_addr = "192.168.35.130"){ #此IP为不合法
11 set $rewrite false;
12 }
13 #非法IP进行判断,打上标记
14 if ($rewrite = true){
15 rewrite (.+) /maintenance.html;
16 }
17 #匹配标记进行跳转
18 location = /maintenance.html {
19 root /usr/share/nginx/html;
20 }
2.给maintenance.html添加自定义页面内容
[root@localhost named]# cd /usr/share/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# vim maintenance.html
<h1>this is test web</h1>
3.重启服务