zoukankan      html  css  js  c++  java
  • shopex-百度爬虫抓取过于频繁导致php-cgi占用CPU过高的解决办法

    步骤

    1、开启slowlog:php-fpm里修改配置

    观察slowlog里的超时文件,然后修改相应超时文件

    2、1修改完后,仍然无效,查看access.log,发现大量如下的请求

    220.181.108.*** - - [22/Oct/2018:14:32:45 +0800] "GET /?gallery-358-s14%2C134_13%2C1_s15%2C135_11%2C0_b%2C34_4%2C9_10%2C0_9%2C3_1%2C0_3%2C1_12%2C0_8%2C0-3--1--index.html HTTP/1.1" 403 162 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
    123.125.71.** - - [22/Oct/2018:14:32:45 +0800] "GET /?gallery-358-s14%2C104_13%2C1_s15%2C135_11%2C0_b%2C34_4%2C9_10%2C0_9%2C3_8%2C0_5%2C0_3%2C1_12%2C0-3--1--index.html HTTP/1.1" 403 162 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"

    原来是百度的爬虫不断的抓取分类页面的,筛选链接。官方的的筛选链接没有加rel="nofollow"

    3、在相关页面加上rel="nofollow"后,清空缓存。仍然无效。修改nginx的配置,禁止Baiduspider请求页面

    #禁止Scrapy等工具的抓取
    if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
    return 403;
    }

    #禁止指定UA及UA为空的访问
    if ($http_user_agent ~ "Baiduspider|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;
    }

    #禁止非GET|HEAD|POST方式的抓取
    if ($request_method !~ ^(GET|HEAD|POST)$) {
    return 403;
    }

    4、重启nginx后,终于CPU降下来了。

    后记:

    1、这种做法完全禁止了百度抓取,显示是不行的。可以采用以下方法,限制百度抓取的频率

    百度蜘蛛抓取量骤增,导致服务器负载很高。最终用nginx的ngx_http_limit_req_module模块限制了百度蜘蛛的抓取频率。每分钟允许百度蜘蛛抓取200次,多余的抓取请求返回503。
    nginx的配置:
    #全局配置
    limit_req_zone $anti_spider zone=anti_spider:60m rate=200r/m;
    #某个server中
    limit_req zone=anti_spider burst=5 nodelay;
    if ($http_user_agent ~* "baiduspider") {
    set $anti_spider $http_user_agent;
    }

    参数说明:
    指令limit_req_zone 中的rate=200r/m 表示每分钟只能处理200个请求。
    指令limit_req 中的burst=5 表示最大并发为5。即同一时间只能同时处理5个请求。
    指令limit_req 中的 nodelay 表示当已经达到burst值时,再来新请求时,直接返回503
    IF部分用于判断是否是百度蜘蛛的user agent。如果是,就对变量$anti_spider赋值。这样就做到了只对百度蜘蛛进行限制了。
    详细的参数说明,可以查看官方文档。
    http://nginx.org/en/docs/http/ngx_http_limit_req_module.html#limit_req_zone

    2、待百度抓取页面的缓存更新后,可以去掉对Baiduspider的限制(之前的没有实时生效,应该是分类页百度是用的缓存,没那么快更新,也就没法nofollow,所以设计网站页面时一定要先考虑好SEO的nofollow问题。)

  • 相关阅读:
    leetcode 29-> Divide Two Integers without using multiplication, division and mod operator
    ros topic 发布一次可能会接收不到数据
    python中的print()、str()和repr()的区别
    python 部分函数
    uiautomatorviewer错误 unable toconnect to adb
    pyqt 不规则形状窗口显示
    appium 计算器demo
    Spring 3.0 注解注入详解
    Spring Autowire自动装配
    restful 学习地址
  • 原文地址:https://www.cnblogs.com/showker/p/9830046.html
Copyright © 2011-2022 走看看