#!/bin/bash
NGINX_HOME="/usr/local/nginx"
ACCESS_LOG="$NGINX_HOME/logs/access.log"
cat $ACCESS_LOG >> $NGINX_HOME/logs/access.log-all
echo "" > $NGINX_HOME/conf/blockip.conf
ip_list=$(awk '{print$1}' $ACCESS_LOG | sort -n | uniq -c | awk '$1>2 {print $2}')
if test -z "$ip_list"
then
$NGINX_HOME/sbin/nginx -s reload
else
for ip in $(echo $ip_list)
do
echo "deny" $ip";" >> $NGINX_HOME/conf/blockip.conf
done
$NGINX_HOME/sbin/nginx -s reload
fi
echo "" > $ACCESS_LOG
设置定时任务:crontab -e */1 * * * * 文件名.sh 每分钟执行一次
添加最后一行:include blockip.conf;写入执行文件conf里