本文环境: centos7 lnmp iptables
nginx动态黑名单
原理
nginx配置中可以使用allow/deny ip
来禁止允许ip.
可创建一个blockip.conf,将需要屏蔽的ip保存在里面,再引入。
1 2
| allow 200.200.200.200; deny all;
|
注:
- 全站屏蔽。将
include blockip.conf
放到http{ }语句块。
- 单独站点屏蔽。将
include blockip.conf
放到相应站点的server{ }语句块。
脚本
这里给blockip.conf文件加上了时间,和最后一个是否有效的参数1或者0,以配合iptables解封相应ip,之后可以根据时间解封Ip(未实现)。
nginx日志文件格式为:
1 2 3
| log_format nginxlog2 '$remote_addr $http_x_forwarded_for $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_host" "$http_referer" "$http_user_agent"';
|
具体代码是:
1 2 3 4 5 6 7 8 9
| #!/bin/bash tail -n2000 /data/wwwlogs/access_nginx.log | awk '{print $1,$6,$9,$11,$13}'|awk '{if($1!="200.200.200.200" && $1!="222.222.222.222" && ($2=="\"CONNECT"||$5=="\"-\""||$4!="\"200.200.200.200\"")) print $1}'|sort|uniq -c|sort -rn |awk '{if($1>1) print "deny "$2 "; # " strftime("%Y%m%d%H%M%S",systime()) " 1" }' >> /usr/local/nginx/conf/blockip.conf # 去掉重复ip sort -t' ' -k2,2 -k4,4 -k5,5 /usr/local/nginx/conf/blockip.conf | sort -t' ' -k2,2 -u |awk '{print $1,$2,$3,$4,$5}'> /usr/local/nginx/conf/blockiptmp.conf #这里直接写入blockip.conf会变成0kb,所以用个临时文件
# 重启nginx #/usr/bin/systemctl reload nginx service nginx reload
|
然后添加到定时任务
1
| 27 */6 * * * root /usr/bin/sh /opt/shell/blockip.sh #service crond reload
|
通过iptables动态禁用ip
读取blockip.conf文件(ip唯一)然后根据最后一个参数为1或者0进行增加或者取消限制。
1 2
| tail -n2000 /usr/local/nginx/conf/blockip.conf | awk '{split($2,a,";");print a[1],$4,$5}' | awk '{if($3=="1") system("iptables -D INPUT -s "$1" -j DROP;iptables -I INPUT -s "$1" -j DROP")}' tail -n2000 /usr/local/nginx/conf/blockip.conf | awk '{split($2,a,";");print a[1],$4,$5}' | awk '{if($3=="0") system("iptables -D INPUT -s "$1" -j DROP")}'
|
注意:通过iptables也可以使用以下语句,前者只禁用80端口:
1 2
| iptables -I INPUT -p tcp --dport 80 -s 180.160.220.109 -j DROP iptables -I INPUT -s ***.***.***.*** -j DROP
|
总的脚本
文件名:blockip.sh
1 2 3 4 5 6 7 8 9 10 11
| #!/bin/bash tail -n2000 /data/wwwlogs/access_nginx.log | awk '{print $1,$6,$9,$11,$13}'|awk '{if($1!="200.200.200.200" && $1!="222.222.222.222" && ($2=="\"CONNECT"||$5=="\"-\""||$4!="\"200.200.200.200\"")) print $1}'|sort|uniq -c|sort -rn |awk '{if($1>1) print "deny "$2 "; # " strftime("%Y%m%d%H%M%S",systime()) " 1" }' >> /usr/local/nginx/conf/blockip.conf sort -t' ' -k2,2 -k4,4 -k5,5 /usr/local/nginx/conf/blockip.conf | sort -t' ' -k2,2 -u |awk '{print $1,$2,$3,$4,$5}'> /usr/local/nginx/conf/blockiptmp.conf rm -f /usr/local/nginx/conf/blockip.conf mv /usr/local/nginx/conf/blockiptmp.conf /usr/local/nginx/conf/blockip.conf service nginx reload tail -n2000 /usr/local/nginx/conf/blockip.conf | awk '{split($2,a,";");print a[1],$4,$5}' | awk '{if($3=="1") system("iptables -D INPUT -s "$1" -j DROP;iptables -I INPUT -s "$1" -j DROP")}' tail -n2000 /usr/local/nginx/conf/blockip.conf | awk '{split($2,a,";");print a[1],$4,$5}' | awk '{if($3=="0") system("iptables -D INPUT -s "$1" -j DROP")}' service iptables save service iptables reload
|
定时任务crontab -e
1
| 27 */7 * * * /root/projects/mcosx/blockip.sh > /dev/null 2>&1
|