【问题标题】:Nginx don't block bot by user-agentNginx 不会通过用户代理阻止机器人
【发布时间】:2017-06-25 12:55:21
【问题描述】:

我正在尝试通过用户代理禁止烦人的机器人。我把它放到 nginx 配置的服务器部分:

server {

    listen 80 default_server;

    ....

    if ($http_user_agent ~* (AhrefsBot)) {
       return 444;
    }

通过 curl 检查:

    [root@vm85559 site_avaliable]# curl -I -H 'User-agent: Mozilla/5.0 (compatible; AhrefsBot/5.2; +http://ahrefs.com/robot/)' localhost/
curl: (52) Empty reply from server

所以我检查了 /var/log/nginx/access.log,我看到一些连接得到 444,但另一个连接得到 200!

51.255.65.78 - - [25/Jun/2017:15:47:36 +0300 - -] "GET /product/kovriki-avtomobilnie/volkswagen/?PAGEN_1=10 HTTP/1.1" 444 0 "-" "Mozilla/5.0 (compatible; AhrefsBot/5.2; +http://ahrefs.com/robot/)" 1498394856.155
217.182.132.60 - - [25/Jun/2017:15:47:50 +0300 - 2.301] "GET /product/bryzgoviki/toyota/ HTTP/1.1" 200 14500 "-" "Mozilla/5.0 (compatible; AhrefsBot/5.2; +http://ahrefs.com/robot/)" 1498394870.955

这怎么可能?

【问题讨论】:

    标签: nginx user-agent


    【解决方案1】:

    好的,知道了! 我已将 $server_name 和 $server_addr 添加到 nginx 日志格式,并看到狡猾的机器人通过 ip 连接而没有 server_name:

    51.255.65.40 - _ *myip* - [25/Jun/2017:16:22:27 +0300 - 2.449] "GET /product/soyuz_96_2/mitsubishi/l200/ HTTP/1.1" 200 9974 "-" "Mozilla/5.0 (compatible; AhrefsBot/5.2; +http://ahrefs.com/robot/)" 1498396947.308
    

    所以我添加了这个,机器人无法再连接

    server {
                listen *myip*:80;
                server_name _;
                return 403;
            }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2013-10-20
    相关资源
    最近更新 更多