Nginx禁止ip/网段访问站点
网络上总有那么一些无聊的人或者有目的的人给你来点麻烦。咱得打消他的企图才行。一、Nginx的ngx_http_access_module 模块可以用来设置允许/禁止哪些ip或ip段访问
首先建立下面的配置文件放在nginx的conf目录下面,命名为deny.ip
[*]catdeny.ip
[*]deny 192.168.1.11;
[*]deny 192.168.1.123;
[*]deny 10.0.1.0/24;
在nginx的配置文件nginx.conf中加入:include deny.ip;
重启一下nginx的服务:/usr/local/nginx/sbin/nginx-s>
deny.ip 的格式中也可以用deny all;
如果你想实现这样的应用,除了几个IP外,其他全部拒绝,
那需要你在deny.ip 中这样写
[*]allow 1.1.1.1;
[*]allow 1.1.1.2;
[*]deny all;
如果想只针对某个网站,可以在具体的网站的配置中加入:
[*]location / {
[*]allow 192.168.0.0/24;
[*]deny all;
[*]}
这样就只允许192.168.0.0网段的ip访问,其他ip访问会返回一个403错误。
还可以自定义一个403错误的页面,可以在/usr/local/nginx/html下新建个error403.html文件,里面按照html的语法写个文档,写上一些说明文字。
然后编辑nginx.conf,加入:
[*]error_page 403/error403.html;
[*]location = /error403.html {
[*] root html;
[*]}
二、用IPTABLES规则禁止访问
iptables -I INPUT -p tcp –dport 80 -m –mac-soruce$MAC -j DROP #基于mac地址的
iptables -I INPUT -p tcp –dport 80 -s $IP -j DROP #基于ip地址的
页:
[1]