定时任务执行,对指定url请求的来源IP进行分组和排序,请求次数大于100的输出到黑名单中ip.black中,nginx.conf中默认引入了黑名单文件。
#nginx.conf
#include ip.black ;
mkdir -p /opt/scripts/
touch /opt/scripts/nginx_deny_ip.sh
chmod +x /opt/scripts/nginx_deny_ip.sh
#!/bin/bash
grep '/api/customer/personal/send/code' /opt/logs/nginx/api.log | awk '{print $1}' | sort | uniq -c | sort -rn |awk '{if($1>100)print "deny "$2";"}' >/usr/local/nginx/conf/ip.black;
/etc/init.d/nginx reload