nginx封ip,禁用IP段的设置说明
2016-06-21 10:44
555 查看
原始地址: http://outofmemory.cn/code-snippet/3393/nginx-deny-ip-or-deny-network-subnets
nginx的ngx_http_access_module 模块可以封配置内的ip或者ip段,语法如下:
如果规则之间有冲突,会以最前面匹配的规则为准。
首先要建一个封ip的配置文件blockips.conf,然后vi blockips.conf编辑此文件,在文件中输入要封的ip。
然后保存此文件,并且打开nginx.conf文件,在http配置节内添加下面一行配置:
保存nginx.conf文件,然后测试现在的nginx配置文件是否是合法的:
如果配置没有问题,就会输出:
如果配置有问题就需要检查下哪儿有语法问题,如果没有问题,需要执行下面命令,让nginx重新载入配置文件。
上面配置中禁止了192.168.1.1,允许其他内网网段,然后
然后输入403的文件内容,例如:
如果启用了SSI,可以在403中显示被封的客户端ip,如下:
保存error403文件,然后打开nginx的配置文件
然后保存配置文件,通过
nginx的ngx_http_access_module 模块可以封配置内的ip或者ip段,语法如下:
deny IP; deny subnet; allow IP; allow subnet; # block all ips deny all; # allow all ips allow all;
如果规则之间有冲突,会以最前面匹配的规则为准。
如何配置禁用ip或ip段呢?
下面说明假定nginx的目录在/usr/local/nginx/首先要建一个封ip的配置文件blockips.conf,然后vi blockips.conf编辑此文件,在文件中输入要封的ip。
deny 1.2.3.4; deny 91.212.45.0/24; deny 91.212.65.0/24;
然后保存此文件,并且打开nginx.conf文件,在http配置节内添加下面一行配置:
include blockips.conf;
保存nginx.conf文件,然后测试现在的nginx配置文件是否是合法的:
/usr/local/nginx/sbin/nginx -t
如果配置没有问题,就会输出:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful
如果配置有问题就需要检查下哪儿有语法问题,如果没有问题,需要执行下面命令,让nginx重新载入配置文件。
/usr/local/nginx/sbin/nginx -s reload
如何禁止所有外网ip,仅允许内网ip呢?
如下配置文件location / { # block one workstation deny 192.168.1.1; # allow anyone in 192.168.1.0/24 allow 192.168.1.0/24; # drop rest of the world deny all; }
上面配置中禁止了192.168.1.1,允许其他内网网段,然后
deny all禁止其他所有ip。
如何格式化nginx的403页面呢?
首先执行下面的命令:cd /usr/local/nginx/html vi error403.html
然后输入403的文件内容,例如:
<html> <head><title>Error 403 - IP Address Blocked</title></head> <body> Your IP Address is blocked. If you this an error, please contact webmaster with your IP at webmaster@example.com </body> </html>
如果启用了SSI,可以在403中显示被封的客户端ip,如下:
Your IP Address is <!--#echo var="REMOTE_ADDR" --> blocked.
保存error403文件,然后打开nginx的配置文件
vi nginx.conf,在server配置节内添加下面内容
# redirect server error pages to the static page error_page 403 /error403.html; location = /error403.html { root html; }
然后保存配置文件,通过
nginx -t命令测试配置文件是否正确,若正确通过
nginx -s reload载入配置。
相关文章推荐
- Nginx Rewrite规则
- Nginx禁止未定义的域名访问
- ftp+nginx+lua_Nginx+GraphicsMagick来实现目录浏览、实时缩略图
- Nginx服务的ssl认证和htpasswd认证
- ubuntu 16 安装django nginx uWSGI
- Nginx介绍及安装配置
- (转)Nginx配置和内核优化 实现突破十万并发
- 让Nginx处理Django的静态文件
- linux 安装nginx
- centos 6.5下安装mysql+nginx+redmine 3.1.0 笔记
- 四:深入Nginx之事件和连接 (之一)
- centos7 服务器安装nginx,mysql,php
- 使用nginx lua实现网站统计中的数据收集
- Linux系统安装Nginx教程
- Nginx负载均衡配置实例详解
- 玩玩负载均衡---在window与linux下配置nginx
- Fastdfs与Nginx的整合
- Fastdfs与Nginx的整合
- 三:深入理解Nginx的模块化 (结合源码详解)
- CentOS6.5使用yum命令方便快捷安装Nginx