本文共 2246 字,大约阅读时间需要 7 分钟。
我们都知道网络上的爬虫非常多,有对网站收录有益的,比如百度蜘蛛(Baiduspider),也有不但不遵守robots规则对服务器造成压力,还不能为网站带来流量的无用爬虫,比如宜搜蜘蛛(YisouSpider),于是整理收集了网络上各种禁止垃圾蜘蛛爬站的方法,来防止自己的网站不被其他人爬取呢?通过Nginx, 我们可以拦截大部分爬虫。那么再增加一个反爬虫配额文件
通过修改.htaccess
文件
进入到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 ~ "Googlebot/2.1|AhrefsBot/7.0|Baiduspider/2.0|Baiduspider-render/2.0|bingbot/2.0|BLEXBot/1.0|SemrushBot/7~bl|coccocbot-web/1.0|MJ12bot/v1.4.8|YandexBot/3.0|Bytespider|BLEXBot|bingbot|Nimbostratus|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;}#禁止非GET|HEAD|POST方式的抓取if ($request_method !~ ^(GET|HEAD|POST)$) { return 403;}
然后,在网站相关配置中的 location / {
之后插入如下代码:
include agent_deny.conf;
例如: location /face { #这个位置新增1行: include agent_deny.conf; rewrite ^/sitemap_360_sp.txt$ /sitemap_360_sp.php last; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://xxx.com$request_uri;;}location ~ /\.ht { deny all;}#禁止访问.htxxx文件}保存后,执行如下命令,平滑重启nginx即可:/usr/local/nginx/sbin/nginx -s reload
如果是vps,那非常简单,使用curl -A 模拟
抓取即可,比如:
# -A表示User-Agent# -X表示方法: POST/GET# -I表示只显示响应头部curl -X GET -I -A 'YYSpider' xxx.com模拟宜搜蜘蛛抓取:curl -I -A 'YisouSpider' xxx.com模拟UA为空的抓取:curl -I -A '' xxx.com模拟百度蜘蛛的抓取:curl -I -A 'Baiduspider' xxx.com
结果返回是403禁止访问标识,说明生效!
下面是网络上常见的垃圾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 无用爬虫
转载地址:http://phelf.baihongyu.com/