nginx如何防止cc攻击

作者:袖梨 2026-08-29

img_6a921e620c05430.webp

在nginx中预防cc攻击的方法

用户在正常访问网站时,与服务器建立的连接一般不会超过20个,可以通过以下脚本禁止连接数过大的IP访问。

#!/bin/shstatus=`netstat -na|awk '$5 ~ /[0-9]+:[0-9]+/ {print $5}' |awk -F ":" -- '{print $1}' |sort -n|uniq -c |sort -n|tail -n 1`NUM=`echo $status|awk '{print $1}'`IP=`echo $status|awk '{print $2}'`result=`echo "$NUM > 150" | bc` 当IP的访问次数超过150时,对该IP进行屏蔽if [ $result = 1 ]thenecho IP:$IP is over $NUM, BAN IT!/sbin/iptables -I INPUT -s $IP -j DROPfi

相关文章

精彩推荐