寶塔面板搭建的網(wǎng)站查看網(wǎng)站日志時(shí):我們會(huì)經(jīng)常發(fā)現(xiàn)一些空UA采集,或者垃圾爬蟲(chóng)高頻的訪問(wèn),導(dǎo)致CPU消耗過(guò)高,其實(shí)通過(guò)User Agent的特征,我們可以禁止那些惡意的無(wú)效的訪問(wèn),Nginx環(huán)境如何禁止特定爬行工具、空UA、特定UA訪問(wèn)我們的網(wǎng)站:
下面的四項(xiàng)都是nginx配置在server內(nèi)
1、禁止指定UA及UA為空的訪問(wèn)
if ($http_user_agent ~ "FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$" ) {
return 403;
}
2、禁止非GET|HEAD|POST方式的抓取
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 403;
}
3、禁止Scrapy等工具的抓取
if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
return 403;
}
4、此方法最簡(jiǎn)單粗暴,直接網(wǎng)站 Nginx 配置文件里面server內(nèi)添加以下的規(guī)則,針對(duì)服務(wù)器上經(jīng)常被掃描的文件后綴,直接返回404,資源下載網(wǎng)站請(qǐng)根據(jù)情況調(diào)整規(guī)則
# 禁止訪問(wèn)指定文件
location ~ .(zip|rar|sql|tar|gz|7z)$ {
return 404;
}
寶塔面板的Nginx環(huán)境,登錄寶塔面板后臺(tái) - 網(wǎng)站 -網(wǎng)站設(shè)置 - 配置文件 里面,添加你需要的代碼:

5、在網(wǎng)站根目錄里建立robots.txt,添加如下內(nèi)容
User-agent: YisouSpiderDisallow: /
6、禁止某個(gè)目錄執(zhí)行腳本
【單nginx下有效,如使用了nginx+apache組合,php交給apache處理,需要通過(guò).htaccess進(jìn)行限制】,添加到location ~ .php 之前
location ~* ^/(uploads|templets|data)/.*.(php|php5)$ {
return 444;
}
7、屏蔽某個(gè)IP或IP段
#屏蔽192.168.5.23這個(gè)IP
deny 192.168.5.23;
#屏蔽192.168.5.* 這個(gè)段
deny 192.168.5.0/24;
8、此種方法比較暴力,就是以彼之道,還施彼身,利用偽靜態(tài)規(guī)則進(jìn)行跳轉(zhuǎn)下載,比如:掃描根目錄下的 /web.rar,那么就會(huì)觸發(fā)規(guī)則跳轉(zhuǎn)到大文件下載地址。
打開(kāi)網(wǎng)站 Nginx 配置文件,將規(guī)則加入 server 內(nèi),示例:
server {
...
# 專治掃描戶
rewrite .rar/?$ http://speedtest.tele2.net/100GB.zip permanent;
rewrite .tar/?$ http://speedtest.tele2.net/100GB.zip permanent;
rewrite .zip/?$ http://speedtest.tele2.net/100GB.zip permanent;
rewrite .sql/?$ http://speedtest.tele2.net/100GB.zip permanent;
rewrite .gz/?$ http://speedtest.tele2.net/100GB.zip permanent;
rewrite .7z/?$ http://speedtest.tele2.net/100GB.zip permanent;
# 或者使用
rewrite .(rar|zip|tar|sql|gz|7z)/?$ http://speedtest.tele2.net/100GB.zip permanent;
...
}
如果覺(jué)得 100GB 響應(yīng)時(shí)間有點(diǎn)長(zhǎng),那么可以替換小文件下載地址:
10G:
新加坡: http://lg-sin.fdcservers.net/10GBtest.zip
日本: http://lg-tok.fdcservers.net/10GBtest.zip
香港: http://lg-hkg.fdcservers.net/10GBtest.zip
1G:
日本:http://hnd-jp-ping.vultr.com/vultr.com.1000MB.bin
新加坡:https://sgp-ping.vultr.com/vultr.com.1000MB.bin
如何測(cè)試效果?
Linux的vps,比如Centos7 使用Xshell軟件連接上VPS。(如何使用Xshell通過(guò)SSH連接管理VPS)
使用使用下面的命令測(cè)試一下就行:
模擬UA為空的抓?。?br data-filtered="filtered" style="font-family: "Microsoft YaHei", Arial, "Heiti SC"; font-size: medium; white-space: normal; background-color: rgb(255, 255, 255);"/>curl -I -A '' https://www.78moban.com
模擬垃圾爬蟲(chóng)AhrefsBot抓?。?br data-filtered="filtered" style="font-family: "Microsoft YaHei", Arial, "Heiti SC"; font-size: medium; white-space: normal; background-color: rgb(255, 255, 255);"/>curl -I -A 'AhrefsBot' https://www.78moban.com
模擬AhrefsBot訪問(wèn)返回:HTTP/1.1 403 Forbidden 則設(shè)置生效訪問(wèn)被攔截!
模擬百度蜘蛛的抓?。?br data-filtered="filtered" style="font-family: "Microsoft YaHei", Arial, "Heiti SC"; font-size: medium; white-space: normal; background-color: rgb(255, 255, 255);"/>curl -I -A 'Baiduspider' https://www.78moban.com
模擬百度蜘蛛訪問(wèn)則成功返回:HTTP/1.1 200 OK
表示百度的爬蟲(chóng)是正??梢栽L問(wèn)你的網(wǎng)站。
附一部分垃圾垃圾爬蟲(chóng)和掃描
FeedDemon 內(nèi)容采集
BOT/0.1 sql注入
CrawlDaddy sql注入
Java 內(nèi)容采集
Jullo 內(nèi)容采集
Feedly 內(nèi)容采集
UniversalFeedParser 內(nèi)容采集
ApacheBench cc攻擊器
Swiftbot 無(wú)用爬蟲(chóng)
YandexBot 無(wú)用爬蟲(chóng)
AhrefsBot 無(wú)用爬蟲(chóng)
YisouSpider 無(wú)用爬蟲(chóng)
jikeSpider 無(wú)用爬蟲(chóng)
MJ12bot 無(wú)用爬蟲(chóng)
ZmEu phpmyadmin 漏洞掃描
WinHttp 采集cc攻擊
EasouSpider 無(wú)用爬蟲(chóng)
HttpClient tcp攻擊
Microsoft URL Control 掃描
YYSpider 無(wú)用爬蟲(chóng)
jaunty wordpress爆破掃描器
oBot 無(wú)用爬蟲(chóng)
Python-urllib 內(nèi)容采集
Indy Library 掃描
FlightDeckReports Bot 無(wú)用爬蟲(chóng)
Linguee Bot 無(wú)用爬蟲(chóng)
我是有底線的
掃描二維碼手機(jī)查看該文章
文章引用:http://www.qingbaosc.com/news/webzhishi/1404.html




