基于客户端 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.重启服务
[root@localhost html]# systemctl restart nginx.service
4.在win7上进行访问,此时win7的IP为不合法的,去进行域名访问,会跳转到指定的页面去

5.在win7上把IP改为合法IP,再去进行访问,就会正常访问页面


本文介绍如何使用Nginx实现基于客户端IP的访问控制,当IP不合法时重定向至维护页面,仅允许特定IP(如192.168.35.130)正常访问。操作包括修改Nginx主配置文件、定制维护页面、服务重启及验证不同IP的访问效果。
&spm=1001.2101.3001.5002&articleId=103109144&d=1&t=3&u=21a841bb2e6346b9a27a8f3249005269)
3020

被折叠的 条评论
为什么被折叠?



