1、添加IP白名单文件
在nginx目录的 conf
中添加文件 ip.conf
,注意白名单文件不用添加任何注释,可以有空行
vi ip.conf
192.168.3.11 1;192.168.3.10 1;
192.168.0.112 1;
2、配置nginx.conf
编辑http节点:
http {# ...# geo IP whitelistgeo $remote_addr $ip_whitelist {default 0;include ip.conf;}# ...
}
编辑server节点:
server {listen 80;# ...# IP whitelistset $whitelist_flag 1;if ( $ip_whitelist != 1 ) {set $whitelist_flag "${whitelist_flag}0";}if ( $request_uri !~* '/warn_navigate_page' ) {set $whitelist_flag "${whitelist_flag}0";}if ( $whitelist_flag = "100" ) {#return 403;rewrite ^(.*)$ $scheme://$host:$server_port/warn_navigate_page break; #白名单的提示页面}# ...
}
也可以在location节点中编辑,示例:
编辑location节点:
location /test {proxy_pass http://IP/test;# ...# IP whitelistset $whitelist_flag 1;if ( $ip_whitelist != 1 ) {set $whitelist_flag "${whitelist_flag}0";}if ( $request_uri !~* '/warn_navigate_page' ) {set $whitelist_flag "${whitelist_flag}0";}if ( $whitelist_flag = "100" ) {#return 403;rewrite ^(.*)$ $scheme://$host:$server_port/warn_navigate_page break; #白名单的提示页面}# ...
}
添加导航的提示页 /warn_navigate_page
server {listen 80;# ...# 白名单的提示导航页面location /warn_navigate_page {root /home/java/nginx/bizapp/warn_navigate_page;index warn_navigate_page.html warn_navigate_page.htm;rewrite ^(.*)$ /warn_navigate_page.html break;}
}
3、编辑白名单的提示导航页面
在 /home/java/nginx/bizapp/warn_navigate_page
中编辑页面warn_navigate_page.html
参考:
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"><meta content="yes" name="apple-mobile-web-app-capable"><meta content="black" name="apple-mobile-web-app-status-bar-style"><meta content="telephone=no" name="format-detection"><meta content="email=no" name="format-detection"><title>系统通知</title><style type="text/css">body {background: url(https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png) no-repeat;background-size: 100% 100%;background-attachment: fixed;}</style>
</head><body><div><pre> 【通知公告】尊敬的用户您好,系统已不提供IP地址直接访问,请联系管理员添加白名单。互联网的域名访问地址:<a href="https://www.baidu.com">跳转https://www.baidu.com</a></pre></div></body>
<script type="text/javascript">
</script></html>
预计效果图