zoukankan      html  css  js  c++  java
  • nginx限制蜘蛛的频繁抓取

    蜘蛛抓取量骤增,导致服务器负载很高。最终用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;
    }
    
    #其它爬虫限制参考
     if ($http_user_agent ~* "qihoobot|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! 
    Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot
    ") { 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

    这个模块对请求的限制采用了漏桶算法。
    漏桶算法详见 http://baike.baidu.com/view/2054741.htm
    相关代码请查看nginx源码文件 src/http/modules/ngx_http_limit_req_module.c
    代码的核心部分是ngx_http_limit_req_lookup 方法。

  • 相关阅读:
    ETL之数据库
    Git的简单实用
    Linux-easy mock部署
    Linux-docker安装mysql
    Linux-安装docker
    Linux-centos7安装Python3和pip3
    Linux-VMware下安装centos7
    Python之hasattr()、getattr()和setattr()
    jsonpath 信息抽取类库
    Python之内置测试框架unittest
  • 原文地址:https://www.cnblogs.com/xiewenming/p/8108703.html
Copyright © 2011-2022 走看看