除了前面361源碼介紹的WDPC面板外,寶塔面板現在很多小白用戶使用的非常多,功能比較簡單易用,但是其中防御功能較弱,內置的WAF并不能實現服務器級別的防火墻封IP,所以我們只能借用一下其中的功能,加上LINUX系統天然自帶的SHELL+IPTABLES防火墻來封禁IP
寶塔安裝的NGINX按下面配置
軟件管理-Nginx管理-配置修改
將
error_log /www/wwwlogs/nginx_error.log crit;
改成
error_log /www/wwwlogs/nginx_error.log error;
然后到需要開啟防御的網站管理 點 設置-流量限制
這里啟用流量限制,并將單IP并發數進行限制,一般如果網站頁面不復雜設置為10就行。如果網頁有很多調用CSS,JS之類的,自行計算一下
這樣重啟 NGINX 后當有客戶端頻繁刷新你的網頁,發起的請求將在超過設置的每秒鐘允許數量后被禁止訪問,同時將在 NGINX error.log(默認在 /www/wwwroot/nginx_error.log) 中看到類似記錄:
2018/04/27 14:25:27 [error] 6307#0: *1472746 limiting connections by zone "perip", client: 182.161.35.139, server: 104.153.102.68, request: "GET /index.php?10=8 HTTP/1.1"
此時請求已經被 NGINX 限流,但是客戶端仍然能夠繼續發送請求到NGINX,還是會占用一定的服務器資源。
因此接下來進行shell腳本設置,將這個client的IP直接通過防火墻封殺
進CENTOS
vi /www/wwwlogs/iptables.sh
shell腳本如下
#!/bin/bashtail /www/wwwlogs/nginx_error.log -n 200 | grep "perip"| awk -F'[ ,]' '{print $13}' | sort | uniq | sort -n > drop_ip.txt for i in $(cat /www/wwwlogs/drop_ip.txt)do FLAG=0 for j in $(cat /www/wwwlogs/drop_ip_all.txt)do if [ $i = $j ]; then FLAG=1 break fi done if [ $FLAG -eq 0 ]; then echo --new drop ip:$i #add to iptables /sbin/iptables -C INPUT -s $i -j DROP fi done #select drop_ip.txt append to drop_ip_all.txt cat drop_ip.txt >> drop_ip_all.txt #drop_ip_all remove repeat ip #cat drop_ip_all.txt | sort | uniq > drop_ip_all.txt
chmod +x /www/wwwlogs/iptables.shecho "* * * * * root /www/wwwlogs/iptables.sh" >> /etc/crontabservice crond restart
還可以設置每天凌晨重啟防火墻
echo "01 1 * * * root /etc/init.d/iptables restart" >> /etc/crontabservice crond restart
當然如果你是套了CDN,那也是有解決辦法的,但是需要你的CDN提供商支持IP防火墻,也就是IP黑名單功能,比如我們合作的Cloudflare就支持這個功能。
以上就是寶塔面板Nginx配合shell腳本實現自動封禁IP 防御CC DDOS攻擊方法(轉)的全部內容,希望對大家的學習和解決疑問有所幫助,也希望大家多多支持武林網。新聞熱點
疑難解答