除了前面361源碼介紹的WDPC面板外,寶塔面板現(xiàn)在很多小白用戶使用的非常多,功能比較簡(jiǎn)單易用,但是其中防御功能較弱,內(nèi)置的WAF并不能實(shí)現(xiàn)服務(wù)器級(jí)別的防火墻封IP,所以我們只能借用一下其中的功能,加上LINUX系統(tǒng)天然自帶的SHELL+IPTABLES防火墻來(lái)封禁IP
寶塔安裝的NGINX按下面配置

軟件管理-Nginx管理-配置修改
將
error_log /www/wwwlogs/nginx_error.log crit;
改成
error_log /www/wwwlogs/nginx_error.log error;
然后到需要開(kāi)啟防御的網(wǎng)站管理 點(diǎn) 設(shè)置-流量限制
這里啟用流量限制,并將單IP并發(fā)數(shù)進(jìn)行限制,一般如果網(wǎng)站頁(yè)面不復(fù)雜設(shè)置為10就行。如果網(wǎng)頁(yè)有很多調(diào)用CSS,JS之類的,自行計(jì)算一下
這樣重啟 NGINX 后當(dāng)有客戶端頻繁刷新你的網(wǎng)頁(yè),發(fā)起的請(qǐng)求將在超過(guò)設(shè)置的每秒鐘允許數(shù)量后被禁止訪問(wèn),同時(shí)將在 NGINX error.log(默認(rèn)在 /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"
此時(shí)請(qǐng)求已經(jīng)被 NGINX 限流,但是客戶端仍然能夠繼續(xù)發(fā)送請(qǐng)求到NGINX,還是會(huì)占用一定的服務(wù)器資源。
因此接下來(lái)進(jìn)行shell腳本設(shè)置,將這個(gè)client的IP直接通過(guò)防火墻封殺
進(jìn)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.txtchmod +x /www/wwwlogs/iptables.shecho "* * * * * root /www/wwwlogs/iptables.sh" >> /etc/crontabservice crond restart
還可以設(shè)置每天凌晨重啟防火墻
echo "01 1 * * * root /etc/init.d/iptables restart" >> /etc/crontabservice crond restart
當(dāng)然如果你是套了CDN,那也是有解決辦法的,但是需要你的CDN提供商支持IP防火墻,也就是IP黑名單功能,比如我們合作的Cloudflare就支持這個(gè)功能。
以上就是寶塔面板Nginx配合shell腳本實(shí)現(xiàn)自動(dòng)封禁IP 防御CC DDOS攻擊方法(轉(zhuǎn))的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)和解決疑問(wèn)有所幫助,也希望大家多多支持武林網(wǎng)。新聞熱點(diǎn)
疑難解答