Pixiv - KiraraShss
984 字
5 分钟
Apache-Nginx-PHP屏蔽垃圾UA爬虫实战指南
最近发现手上有个站点访问速度非常慢,看了nginx 日志发现有好多宜搜等垃圾的抓取记录,这些垃圾爬虫既不遵守 robots 规则对服务器造成压力,还不能为网站带来流量的无用爬虫,于是从网络上整理收集了各种禁止垃圾蜘蛛爬站的方法,留做记录,以备不时之需。

Apache
1、通过修改 .htaccess 文件
修改网站目录下的.htaccess,添加如下代码即可(2 种代码任选):
可用代码(1)
RewriteEngine OnRewriteCond %{HTTP_USER_AGENT} (^$|FeedDemon|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|HttpClient|MJ12bot|heritrix|EasouSpider|DotBot|Ezooms) [NC]RewriteRule ^(.*)$ - [F]可用代码(2)
SetEnvIfNoCase ^User-Agent$ .*(FeedDemon|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|HttpClient|MJ12bot|heritrix|EasouSpider|DotBot|Ezooms) BADBOTOrder Allow,DenyAllow from allDeny from env=BADBOT可用代码(3)
如果以上两种方式会导致500错误,可以使用下面的代码。
setEnvIfNoCase User-Agent "^$" bad_botsetEnvIfNoCase User-Agent "^FeedDemon" bad_botsetEnvIfNoCase User-Agent "^Indy Library" bad_botsetEnvIfNoCase User-Agent "^Alex Toolbar" bad_botsetEnvIfNoCase User-Agent "^AskTbFXTV" bad_botsetEnvIfNoCase User-Agent "^AhrefsBot" bad_botsetEnvIfNoCase User-Agent "^CrawlDaddy" bad_botsetEnvIfNoCase User-Agent "^CoolpadWebkit" bad_botsetEnvIfNoCase User-Agent "^Java" bad_botsetEnvIfNoCase User-Agent "^Feedly" bad_botsetEnvIfNoCase User-Agent "^UniversalFeedParser" bad_botsetEnvIfNoCase User-Agent "^ApacheBench" bad_botsetEnvIfNoCase User-Agent "^Microsoft URL Control" bad_botsetEnvIfNoCase User-Agent "^Swiftbot" bad_botsetEnvIfNoCase User-Agent "^ZmEu" bad_botsetEnvIfNoCase User-Agent "^oBot" bad_botsetEnvIfNoCase User-Agent "^jaunty" bad_botsetEnvIfNoCase User-Agent "^Python-urllib" bad_botsetEnvIfNoCase User-Agent "^lightDeckReports Bot" bad_botsetEnvIfNoCase User-Agent "^YYSpider" bad_botsetEnvIfNoCase User-Agent "^DigExt" bad_botsetEnvIfNoCase User-Agent "^HttpClient" bad_botsetEnvIfNoCase User-Agent "^MJ12bot" bad_botsetEnvIfNoCase User-Agent "^heritrix" bad_botsetEnvIfNoCase User-Agent "^EasouSpider" bad_botsetEnvIfNoCase User-Agent "^DotBot" bad_botsetEnvIfNoCase User-Agent "^Ezooms" bad_botDeny from env=bad_bot2、通过修改 httpd.conf 配置文件
找到如下类似位置,根据以下代码 新增 / 修改,然后重启 Apache 即可:
DocumentRoot /home/wwwroot/xxx<Directory "/home/wwwroot/xxx">SetEnvIfNoCase User-Agent ".*(FeedDemon|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|HttpClient|MJ12bot|heritrix|EasouSpider|DotBot|Ezooms)" BADBOT Order allow,deny Allow from all deny from env=BADBOT</Directory>Nginx
进入到 nginx 安装目录下的 conf 目录,将如下代码保存为 agent_deny.conf
cd /usr/local/nginx/confvim agent_deny.conf复制下面的代码并保存
#禁止Scrapy等工具的抓取if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) { return 403;}#禁止指定UA及UA为空的访问if ($http_user_agent ~* "FeedDemon|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|HttpClient|MJ12bot|heritrix|EasouSpider|DotBot|Ezooms|^$" ) { return 403;}#禁止非GET|HEAD|POST方式的抓取if ($request_method !~ ^(GET|HEAD|POST)$) { return 403;}然后,在网站相关配置中的 location / { 之后插入如下代码:
[marsge@Mars_Server ~]$ cat /usr/local/nginx/conf/gowah.conflocation / { try_files $uri $uri/ /index.php?$args; #这个位置新增1行: include agent_deny.conf; rewrite ^/sitemap_360_sp.txt$ /sitemap_360_sp.php last; rewrite ^/sitemap_baidu_sp.xml$ /sitemap_baidu_sp.php last; rewrite ^/sitemap_m.xml$ /sitemap_m.php last;使用:wq保存后,执行如下命令,平滑重启 nginx 即可:
/usr/local/nginx/sbin/nginx -s reloadPHP
将如下方法放到贴到网站入口文件 index.php 中的第一个 <?php 之后即可:
//获取UA信息$ua = $_SERVER['HTTP_USER_AGENT'];//将恶意USER_AGENT存入数组$now_ua = array('FeedDemon ','BOT/0.1 (BOT for JCE)','CrawlDaddy ','Java','Feedly','UniversalFeedParser','ApacheBench','Swiftbot','ZmEu','Indy Library','oBot','jaunty','YandexBot','AhrefsBot','MJ12bot','WinHttp','EasouSpider','HttpClient','Microsoft URL Control','YYSpider','jaunty','Python-urllib','DotBot','lightDeckReports Bot');//禁止空USER_AGENT,dedecms等主流采集程序都是空USER_AGENT,部分sql注入工具也是空USER_AGENTif(!$ua) { header("Content-type: text/html; charset=utf-8"); die('请勿采集本站,因为采集的站长木有小JJ!');}else{ foreach($now_ua as $value )//判断是否是数组中存在的UA if(eregi($value,$ua)) { header("Content-type: text/html; charset=utf-8"); die('请勿采集本站,因为采集的站长木有小JJ!'); }}测试效果
如果是 vps,那非常简单,使用 curl -A 模拟抓取即可,例如模拟 Swiftbot 蜘蛛抓取:
curl -I -A 'Swiftbot' moewah.com模拟 UA 为空的抓取:
curl -I -A '' moewah.com模拟百度蜘蛛的抓取:
curl -I -A 'Baiduspider' moewah.com如果 Swiftbot 和 UA 为空的返回代码为403,而百度蜘蛛成功返回代码200,说明配置成功生效。
垃圾UA收集
FeedDemon 内容采集BOT/0.1 (BOT for JCE) sql注入CrawlDaddy sql注入Java 内容采集Jullo 内容采集Feedly 内容采集UniversalFeedParser 内容采集ApacheBench cc攻击器Swiftbot 无用爬虫YandexBot 无用爬虫AhrefsBot 无用爬虫YisouSpider 无用爬虫(已被UC神马搜索收购,此蜘蛛可以放开!)MJ12bot 无用爬虫ZmEu phpmyadmin 漏洞扫描WinHttp 采集cc攻击EasouSpider 无用爬虫HttpClient tcp攻击Microsoft URL Control 扫描YYSpider 无用爬虫jaunty wordpress爆破扫描器oBot 无用爬虫Python-urllib 内容采集Indy Library 扫描FlightDeckReports Bot 无用爬虫Linguee Bot 无用爬虫DotBot 无用爬虫(国外SEO爬虫)参考网站
问说:http://www.uedsc.com/acquisition.html
浩海:http://www.it300.com/article-15358.html
推荐文章
基于标签匹配 · 智能推荐支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
Apache-Nginx-PHP屏蔽垃圾UA爬虫实战指南
https://blog.moewah.com/posts/12/
喵斯基部落