Nginx 限制百度等爬虫连接

843 查看

目的: 每两秒允许1个来自header头包含Baiduspider等的爬虫类请求

http {

map $http_user_agent $is_limited_bot {
    "~Baiduspider" 1;
    "~OtherSpider" 2;
    default "";
}
limit_req_zone  $is_limited_bot zone=botzone:10m   rate=30r/m;

server {
    ...

    location / {
        ...

        limit_req zone=botzone burst=5;
    }
}

}

测试: curl -I http://example.com -A OtherSpider

Nginx官方文档:http://nginx.org/en/docs/http/ngx_http_limit_req_module.html
中文相关解释:http://storysky.blog.51cto.com/628458/642970/