根据UA设置nginx规则屏蔽网络爬虫访问

内容纲要

过去写博客的时候经常出现服务器宕机,网页全部刷不出来,但是Ping服务器的时候又能Ping通。登录SSH看了下top,惊呆了,平均负载13 12 8。瞬间觉得我这是被人DDOS了么?看了下进程基本上都是php-fpm把CPU给占了,去看下日志吧。。。

201616112517692.png (415×354)

一看不得了,基本上都是被一个User Agent叫"YisouSpider"的东西给刷屏了,一看就不知道是哪的蜘蛛,太没节操了。
找到根目录配置区,增加User Agent过滤判断语句,发现叫"YisouSpider"的直接返回403

注1:如果需要增加多个过滤,这样做

?

1
($http_user_agent ~* "Spider1|Spider2|Spider3|Spider4")

,中间用|隔开就行了

注2:如果你是用的是子目录博客,像我的一样,那么要找到 "location /blog/" 这样的区段去修改

?

1
2
3
4
5
6
location / {
......其它配置
if ($http_user_agent ~* "YisouSpider") {
return 403;
}
}

配置完成wq保存后reload一下nginx,然后使用以下命令自我测试,地址自己改。没装curl的我就没办法了,自己apt或者yum装一个吧,神器来的。

?

1
curl -I -A "YisouSpider" www.slyar.com/blog/

看到返回403就可以了,说明配置成功

201616112538666.png (471×165)

PS:一些常见爬虫的User-Agent,这些一般就不要过滤了~
百度爬虫
* Baiduspider+(+http://www.baidu.com/search/spider.htm”)

google爬虫
* Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
* Googlebot/2.1 (+http://www.googlebot.com/bot.html)
* Googlebot/2.1 (+http://www.google.com/bot.html)

雅虎爬虫(分别是雅虎中国和美国总部的爬虫)
*Mozilla/5.0 (compatible; Yahoo! Slurp China; http://misc.yahoo.com.cn/help.html”)
*Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp”)

新浪爱问爬虫
*iaskspider/2.0(+http://iask.com/help/help_index.html”)
*Mozilla/5.0 (compatible; iaskspider/1.0; MSIE 6.0)

搜狗爬虫
*Sogou web spider/3.0(+http://www.sogou.com/docs/help/webmasters.htm#07″)
*Sogou Push Spider/3.0(+http://www.sogou.com/docs/help/webmasters.htm#07″)

网易爬虫
*Mozilla/5.0 (compatible; YodaoBot/1.0; http://www.yodao.com/help/webmaster/spider/”; )

MSN爬虫
*msnbot/1.0 (+http://search.msn.com/msnbot.htm”)

发表回复