nginx 允许ip访问批量封IP

你可能喜欢
12345678910
关于本站 本站以分享运维技术为主,欢迎大家参与技术分享,同时也欢迎大家吐槽,本站提供以下交流圈:QQ群①:*****(满)QQ群②:6690706 QQ群③: QQ群④:(新) 微信公众号:ttlsacomnginx封ip,禁用IP段的设置说明 - 为程序员服务
为程序员服务
nginx封ip,禁用IP段的设置说明
nginx的ngx_http_access_module 模块可以封配置内的ip或者ip段,语法如下:
# block all ips
# allow all ips
如果规则之间有冲突,会以最前面匹配的规则为准。
如何配置禁用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.
保存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
192.168.1.1;
# allow anyone in 192.168.1.0/24
192.168.1.0/24;
# drop rest of the world
上面配置中禁止了192.168.1.1,允许其他内网网段,然后deny all禁止其他所有ip。
如何格式化nginx的403页面呢?
首先执行下面的命令:
cd /usr/local/nginx/html
vi error403.html
然后输入403的文件内容,例如:
&head&&title&Error 403 - IP Address Blocked&/title&&/head&
Your IP Address is blocked. If you this an error, please contact webmaster with your IP at
如果启用了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
/error403.
location = /error403.html {
然后保存配置文件,通过nginx -t命令测试配置文件是否正确,若正确通过nginx -s reload载入配置。
您可能的代码
相关聚客文章
荣誉:2071
相关专栏文章微信公众号:centoscn
nginx 自动封 ip 过高连接
用命令查看web连接过高的IP地址,但是需要人工智能去封,太麻烦了,直接写个脚本自动解决。web服务器是用nginx,python为2.6  首先在nignx的config中建立空文件deny.ip, 然后在nginx.conf 的http标签中添加“include deny.”。在nginx下sbin的目录中放入自动脚本。脚本可以查到连接最大的IP,并插入屏蔽列表中,验证正确性后导入配置。全部完成或者出错后发送邮件。被封ip再次访问会报403错误,如果不希望报错可以跳转到其它页面。源码如下:check_deny_up.py 启动i脚本#!/bin/python
#-*- coding:utf-8 -*-
# Filename:
# Revision:
simonzhang
www.simonzhang.net
### END INIT INFO
fromstring importstrip
fromemail.mime.text importMIMEText
importsmtplib
check_comm ="/bin/netstat -antp|grep :80|awk ' ''{print $5}'|awk -F: '{print $1}'|sort -r|uniq -c|sort -n -k1 -r"
max_ip =100
mail_host =‘’;
mail_user =‘’;
mail_pwd =‘’;
mail_to =‘’;
mail_cc =‘’;
defreboot_nginx_sendmail(ip_list):
#### reboot nginx
_get_check_confile =os.popen('./nginx -t').readlines()
ifstr(_get_check_confile.find('ok')) !='-1':
os.system('./nginx -s reload')
_mail_content =ip_list
_mail_content ='Error'
#### send mail
msg =MIMEText(_mail_content)
msg['From'] =mail_user
msg['Subject'] =' force ip.'
msg['To'] =mail_to
s =smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user, mail_pwd)
s.sendmail(mail_user, [mail_to, mail_cc], msg.as_string())
exceptException, e:
#### force out IP
defforce_out(_deny_ip):
_write_status =0
_read_force_file =open('../conf/deny.ip', 'rb').read()
ifstr(_read_force_file.find(_deny_ip)) =='-1':
_get_force_file =open('../conf/deny.ip', 'ab')
_get_force_file.write('deny %\n'%_deny_ip)
_get_force_file.close()
_write_status =1
return_write_status
return_write_status
reboot_nginx_sendmail("Error !")
return_write_status
defmain():
get_high_ip =os.popen('%s'%check_comm).readlines()
_count_force_ip =0
_force_ip_list =''
fori inxrange(3):
_get_count =strip(get_high_ip[i]).split(' ')[0]
_get_ip =strip(strip(get_high_ip[i]).split(' ')[1])
_get_count =0
_get_ip =''
# Maximum connection IP is Beyond the limit value
if(int(_get_count) & max_ip) and(len(_get_ip) & 0):
force_ip =_get_ip
_get_status =force_out(force_ip)
# check maximum is added in the deny.ip file
ifstr(_get_status) =='1':
_count_force_ip +=1
_force_ip_list +=' %s '%force_ip
if _count_force_ip & 0:
reboot_nginx_sendmail(_force_ip_list)
if__name__ =='__main__':
main()check_deny_up.sh#! /bin/bash
# make simon-
### END INIT INFO
# Source function library.
. /etc/profile
cd/Data/apps/nginx/sbin/
# See how we were called.
case"$1"in
/usr/local/bin/pythoncheck_ip_deny.py
echo$"Usage: $0 {start}"
exit将启动脚本放在crontab中运行。
------分隔线----------------------------首页 & Linux/unix/mac, 安全/漏洞fail2ban处理nginx下的恶意扫描 最近听说同行公司的数据库被人暴了,老大要求将我们自己的站点也做下检查。在查看nginx日志时,同样发现有人进行过恶意扫描。同时也为了防止别人恶性来公司站点上扒取数据。所以决定通过fail2ban配合iptables自动封掉这些IP。 发现的恶意扫描日志:access.log:112.122.134.43 - - [06/Oct/:22 +0800] "HEAD /flashfxp.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:112.122.134.43 - - [06/Oct/:22 +0800] "HEAD /ip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:112.122.134.43 - - [06/Oct/:23 +0800] "HEAD /1.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:112.14.63.7 - - [06/Oct/:23 +0800] "HEAD /flashfxp.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:114.112.29.44 - - [06/Oct/:23 +0800] "HEAD /flashfxp.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:114.112.29.44 - - [06/Oct/:23 +0800] "HEAD /ip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:114.112.29.44 - - [06/Oct/:23 +0800] "HEAD /1.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:112.122.134.43 - - [06/Oct/:23 +0800] "HEAD /2.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:112.14.63.7 - - [06/Oct/:23 +0800] "HEAD /ip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:112.122.134.43 - - [06/Oct/:23 +0800] "HEAD /wwwroot.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:112.14.63.7 - - [06/Oct/:23 +0800] "HEAD /1.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:114.112.29.44 - - [06/Oct/:23 +0800] "HEAD /2.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:112.122.134.43 - - [06/Oct/:23 +0800] "HEAD /www.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:112.122.134.43 - - [06/Oct/:24 +0800] "HEAD /web.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:210.72.75.212 - - [06/Oct/:24 +0800] "HEAD /flashfxp.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"
access.log:114.112.29.44 - - [06/Oct/:24 +0800] "HEAD /wwwroot.zip HTTP/1.1" 404 0 "-" "Mozilla/4.0 ( MSIE 8.0; Windows NT 6.1; Trident/4.0)" "-"一般有些人会定期将站点进行备份,备份后的文件,往往可能是以上面的名字命名,这些恶意的扫描者,希望通过扫到这些信息后,直接下载,邪恶ing……。 fail2ban的安装在此不再提,我之前有篇日志对其安装方法做过介绍。不想通过rpmforge源进行yum安装的,可以在http://pkgs.repoforge.org/fail2ban/&找到相关的rpm包。不过也可以到&sourceforge 上找源码包进行安装。安装完成后,&在/etc/fail2ban/filter.d/目录下建立nginx.conf文件保存下面的内容:
[Definition]
failregex = &HOST& -.*- .*HTTP/1.* 404 .*$
ignoreregex =注:过滤原理是通过查找一定时期内访问结果为404 的所有IP,并通过定义下面的条件,找出符合该规则的IP 。 打开/etc/fail2ban/jail.local配置文件,在其最后加入下面的内容:
[nginx-get-dos]
enabled = true
port = http,https
filter = nginx
//对应上面过滤规则中我们新建的nginx.conf文件
logpath = /logs/nginx/361way.access.log
//日志文件位置
maxretry = 20
//失败多少次后就封掉IP
findtime = 60
//在多少时间内,查找符合规则的IP
bantime = 3600
//禁止该IP多长时间
action = iptables[name=HTTP, port=http, protocol=tcp]
#ignoreip = 192.168.1.2
//白名单中的IP通过fail2ban-regex命令,查看符合规则的IP :
fail2ban-regex /logs/nginx/361way.access.log /etc/fail2ban/filter.d/nginx.conf
…………………………
…………………………
110.20.60.68 (Mon Oct 22 21:08:26 2012)
123.205.60.22 (Mon Oct 22 21:11:58 2012)
203.202.60.69 (Mon Oct 22 21:14:18 2012)
Date template hits:
0 hit(s): Month Day Hour:Minute:Second
0 hit(s): Weekday Month Day Hour:Minute:Second Year
0 hit(s): Weekday Month Day Hour:Minute:Second
0 hit(s): Year/Month/Day Hour:Minute:Second
77607 hit(s): Day/Month/Year:Hour:Minute:Second
0 hit(s): Year-Month-Day Hour:Minute:Second
0 hit(s): Day-Month-Year Hour:Minute:Second[.Millisecond]
0 hit(s): TAI64N
0 hit(s): Epoch
Success, the total number of match is 35201
However, look at the above section 'Running tests' which could contain important
information.注:以上结果是从日志中批配nginx.conf规则的数,并未对jail.local文件中的规则进行匹配 。
[root@bj1 fail2ban]# fail2ban-client status nginx-get-dos
Status for the jail: nginx-get-dos
|- File list:
/logs/nginx/361way.access.log
|- Currently failed: 1
`- Total failed:
|- Currently banned: 1
`- IP list:
192.168.1.5
`- Total banned:
上面查看到的结果同iptables -L (或iptables -nL)得到的结果对应:
[root@bj1 fail2ban]# iptables -nL
……………………………………
……………………………………
Chain fail2ban-HTTP (1 references)
prot opt source
destination
192.168.1.5
本站的发展离不开您的资助,金额随意,欢迎来赏!
分类: Linux/unix/mac, 安全/漏洞 fail2ban, iptables, python您可能也喜欢fail2ban安装及配置 捐助本站
如您感觉本博客有用,可扫码向本博客捐赠近期文章 Cloudera Manager 5和CDH5离线安装 ansible小结(十 二)磁盘使用率筛选 破解微信朋友圈红包照片 电脑崩溃手机重启的html代码 linux下powerpath对盘与更改盘符名文章归档 文章归档 选择月份 2016年二月 &(1) 2016年一月 &(4) 2015年十二月 &(7) 2015年十一月 &(9) 2015年十月 &(6) 2015年九月 &(7) 2015年八月 &(22) 2015年七月 &(9) 2015年六月 &(15) 2015年五月 &(23) 2015年四月 &(11) 2015年三月 &(22) 2015年二月 &(15) 2015年一月 &(24) 2014年十二月 &(12) 2014年十一月 &(15) 2014年十月 &(18) 2014年九月 &(18) 2014年八月 &(18) 2014年七月 &(20) 2014年六月 &(21) 2014年五月 &(24) 2014年四月 &(11) 2014年三月 &(29) 2014年二月 &(22) 2014年一月 &(22) 2013年十二月 &(24) 2013年十一月 &(20) 2013年十月 &(18) 2013年九月 &(13) 2013年八月 &(15) 2013年七月 &(20) 2013年六月 &(21) 2013年五月 &(19) 2013年四月 &(17) 2013年三月 &(21) 2013年二月 &(17) 2013年一月 &(18) 2012年十二月 &(24) 2012年十一月 &(18) 2012年十月 &(17) 2012年九月 &(17) 2012年八月 &(18) 2012年七月 &(25) 2012年六月 &(36) 2012年五月 &(36) 2012年四月 &(28) 2012年三月 &(46) 2012年二月 &(23) 2012年一月 &(14) 2011年十二月 &(27) 2011年十一月 &(59) 2011年十月 &(19) 2011年九月 &(15) 2011年八月 &(46)nginx封禁ip问题_问答_ThinkSAAS
nginx封禁ip问题
nginx封禁ip问题
请问各位大牛,nginx能直接封禁一个ip一段时间么 ngx_http_limit_req_module这个模块貌似只能限制频率没法封禁ip一段时间。
ip之前我是不知道的,这个ip列表是动态的,没法事先写到配置文件里):
按投票排序
0支持&&/&&0反对
请描述清楚你遇到了什么问题,你做了什么事情来解决
0支持&&/&&0反对
参考这里:
若要动态的修改这个 ip 列表,只需要写个程序,定时去更新 ip 列表文件就行了。
更新之后,校验一下配置文件是否正确,reload nginx 就好了。
还有一种 lua 的实现方法:
0支持&&/&&0反对
用php调用shell脚本更方便,php仅仅是个调用者,所有的事情都是另外的shell脚本做的,这样还安全。
另外或者通过crontab定时执行shell脚本来查询是否有php产生的域名添加任务,这样基本上php和具体执行添加任务的脚本没有明确的调用关系。
0支持&&/&&0反对
你可以使用 php 去修改配置文件,或者使用 php 调用命令来修改配置文件,或者写个 C 程序所有者为 root、权限 u+s 来给 php 调用,以避免直接使用 root 身份运行 php 脚本。
0支持&&/&&0反对
deny 10.9.0.0/16;
PHP开发框架
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
手机客户端
ThinkSAAS接收任何功能的Iphone(IOS)和Android手机的客户端定制开发服务
官方1群:【已满】
让ThinkSAAS更好,把建议拿来。}

我要回帖

更多关于 nginx 允许ip访问 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信