Nginx反爬蟲: 禁止某些User Agent抓取網站


1、在/usr/local/nginx/conf目錄下(因Nginx的安裝區別,可能站點配置文件的路徑有所不同)新建文件deny_agent.config配置文件:

復制代碼
#forbidden Scrapy
if ($http_user_agent ~* (Scrapy|Curl|HttpClient))
{
    return 403;
}

#forbidden UA
if ($http_user_agent ~ "Bytespider|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;
}

#forbidden not GET|HEAD|POST method access
if ($request_method !~ ^(GET|HEAD|POST)$)
{
    return 403;
}
復制代碼

 

2、在對應站點配置文件中包含deny_agent.config配置文件(注意是在server里面):

 

3、重啟Nginx,建議通過nginx -s reload平滑重啟的方式。重啟之前請先使用nginx -t命令檢測配置文件是否正確。

4、通過curl命令模擬訪問,看配置是否生效(返回403 Forbidden,則表示配置OK):

 

附錄:UA收集

復制代碼
FeedDemon             內容采集
BOT/0.1 (BOT for JCE) sql注入
CrawlDaddy            sql注入
Java                  內容采集
Jullo                 內容采集
Feedly                內容采集
UniversalFeedParser   內容采集
ApacheBench           cc攻擊器
Swiftbot              無用爬蟲
YandexBot             無用爬蟲
AhrefsBot             無用爬蟲
YisouSpider           無用爬蟲(已被UC神馬搜索收購,此蜘蛛可以放開!)
jikeSpider            無用爬蟲
MJ12bot               無用爬蟲
ZmEu phpmyadmin       漏洞掃描
WinHttp               采集cc攻擊
EasouSpider           無用爬蟲
HttpClient            tcp攻擊
Microsoft URL Control 掃描
YYSpider              無用爬蟲
jaunty                wordpress爆破掃描器
oBot                  無用爬蟲
Python-urllib         內容采集
Indy Library          掃描
FlightDeckReports Bot 無用爬蟲
Linguee Bot           無用爬蟲

 

#添加如下內容即可防止爬蟲

if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") 

return 403; 

 

#禁止Scrapy等工具的抓取
if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
     return 403;
}
 
#禁止指定UA及UA為空的訪問
if ($http_user_agent ~ "WinHttp|WebZIP|FetchURL|node-superagent|java/|
FeedDemon|Jullo|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|
CrawlDaddy|Java|Feedly|Apache-HttpAsyncClient|UniversalFeedParser|ApacheBench|
Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|
lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|BOT/0.1|
YandexBot|FlightDeckReports|Linguee Bot|^$" ) {
     return 403;             
}

 

 

限制搜索引擎爬蟲頻率

#全局配置
limit_req_zone $anti_spider zone=anti_spider:10m rate=15r/m;

 

#某個server中
 limit_req zone=anti_spider burst=30 nodelay;
 if($http_user_agent ~ "WinHttp|WebZIP|FetchURL|node-superagent|java/|FeedDemon|Jullo|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|Java|Feedly|Apache-HttpAsyncClient|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|BOT/0.1|YandexBot|FlightDeckReports|Linguee Bot|^$" ) {

 set $anti_spider $http_user_agent;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM