nginx封ip,禁用IP段的设置说明
2015-08-10 20:16
579 查看
nginx的ngx_http_access_module模块可以封配置内的ip或者ip段,语法如下:
保存nginx.conf文件,然后测试现在的nginx配置文件是否是合法的:
如果配置没有问题,就会输出:
如果配置有问题就需要检查下哪儿有语法问题,如果没有问题,需要执行下面命令,让nginx重新载入配置文件。
上面配置中禁止了192.168.1.1,允许其他内网网段,然后
然后输入403的文件内容,例如:
如果启用了SSI,可以在403中显示被封的客户端ip,如下:
保存error403文件,然后打开nginx的配置文件
然后保存配置文件,通过
denyIP; denysubnet; allowIP; allowsubnet; #blockallips denyall; #allowallips allowall;如果规则之间有冲突,会以最前面匹配的规则为准。
如何配置禁用ip或ip段呢?
下面说明假定nginx的目录在/usr/local/nginx/首先要建一个封ip的配置文件blockips.conf,然后viblockips.conf编辑此文件,在文件中输入要封的ip。deny1.2.3.4; deny91.212.45.0/24; deny91.212.65.0/24;然后保存此文件,并且打开nginx.conf文件,在http配置节内添加下面一行配置:
includeblockips.conf;
保存nginx.conf文件,然后测试现在的nginx配置文件是否是合法的:
/usr/local/nginx/sbin/nginx-t
如果配置没有问题,就会输出:
theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisok configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful
如果配置有问题就需要检查下哪儿有语法问题,如果没有问题,需要执行下面命令,让nginx重新载入配置文件。
/usr/local/nginx/sbin/nginx-sreload
如何禁止所有外网ip,仅允许内网ip呢?
如下配置文件location/{ #blockoneworkstation deny192.168.1.1; #allowanyonein192.168.1.0/24 allow192.168.1.0/24; #droprestoftheworld denyall; }
上面配置中禁止了192.168.1.1,允许其他内网网段,然后
denyall禁止其他所有ip。
如何格式化nginx的403页面呢?
首先执行下面的命令:cd/usr/local/nginx/html vierror403.html
然后输入403的文件内容,例如:
<html> <head><title>Error403-IPAddressBlocked</title></head> <body> YourIPAddressisblocked.Ifyouthisanerror,pleasecontactwebmasterwithyourIPatwebmaster@example.com </body> </html>
如果启用了SSI,可以在403中显示被封的客户端ip,如下:
YourIPAddressis<!--#echovar="REMOTE_ADDR"-->blocked.
保存error403文件,然后打开nginx的配置文件
vinginx.conf,在server配置节内添加下面内容
#redirectservererrorpagestothestaticpage error_page403/error403.html; location=/error403.html{ roothtml; }
然后保存配置文件,通过
nginx-t命令测试配置文件是否正确,若正确通过
nginx-sreload载入配置。
相关文章推荐
- nginx 直接在配置文章中设置日志分割
- nginx虚拟目录配置(php-fpm+fastcgi)
- nginx配置文件详解
- LNMP的Nginx启动脚本和配置文件
- linux centos6.5安装tengine(nginx)
- LNMP—Nginx的编译安装
- nginx + unicorn 配置
- Nginx配置文件详细说明
- windows配置nginx实现负载均衡集群
- linux 下搭建Nginx+php报错open_basedir restriction in effect. File(/var/www/html/index.php) is not within
- 如何收集 NGINX 指标(第二篇)
- 一台nginx服务器多域名配置 (转)
- LNMP编译安装(centos7+nginx1.9+mysql5.6+php5.5)
- lnmp环境安装(2)-nginx安装
- nginx安装
- N年前Nginx 负载均衡在金山逍遥网中的应用
- Nginx 笔记与总结(10)Nginx 与 PHP 整合
- 01_Nginx安装,nginx下部署项目,nginx.conf配置文件修改,相关文件配置
- 01_Nginx安装,nginx下部署项目,nginx.conf配置文件修改,相关文件配置
- nginx学习记录