Nginx有自己专用的封锁IP的模块ngx_http_access_module ,语法如下:
deny IP;
deny subnet;
allow IP;
allow subnet;
# block all ips
deny all;
# allow all ips
allow all;
如果规则之间有冲突,会以最前面匹配的规则为准。
配置封ip或ip段:
cd到/usr/local/nginx/conf (你 nginx安装的目录,这里是默认的目录)
然后vi banip.conf 新建一个文件
里面代码为
deny 1.2.3.4; //封单个IP
deny 1.2.3.0/24; //封IP段
然后:wq 保存
然后打开nginx.conf
在http节点下增加配置文件:
include banip.conf;
然后:wq 保存
/usr/local/nginx/sbin/nginx -t //测试下是否合法,合法会提示如下:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
然后/usr/local/nginx/sbin/nginx -s reload 重载nginx配置后生效
生效后,被封锁的IP打开网站就提示403错误
美化403提示页面
cd 到/usr/local/nginx/html
然后
vi 403.html
这个文件里输入你美化提示的代码
然后在你想提醒的站点配置文件里,增加如下代码:
error_page 403 /403.html;
location = /403.html {
root html;
allow all;
}
即可
PS:如果想在提示页面里输出客户端IP,使用
<!--#echo var="REMOTE_ADDR" -->
再PS:
美化403提示页面的时候,当前服务器里的资源都不可用,如果有图片,请外链其他服务器的文件,或者把文件传到微博或者微信公众号的素材里
当然,你也可以把这个提示页面设置为跳转到其他链接
周涛博客






评论前必须登录!
注册