ggjge 2014-12-14 09:14 采纳率: 0%
浏览 8495
已结题

nginx如何屏蔽爬虫(YisouSpider、Googlebot等),有酬谢

网站被爬虫爬得扛不住了。。想把这些爬虫都屏蔽了。。

nginx下配有多个站点。。nginx.conf里是没有server字段的。。只有各个站点的.conf文件里有。。

我按照网上的说法,在nginx.conf的server字段(nginx.conf的server字段也是我自己加的)中加入了以下内容:
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") {

return 403;

}

但是用curl -I -A "Googlebot" www.XXX.com,仍然没有返回403。。
太不靠谱了。。
求高手指点。。

另外robots.txt也不好使。。那个东西全靠自觉。。我想要能主动禁止他们。。。因为有的流氓爬虫显然没法用那个解决

nginx.conf内容如下:

#user nobody;
worker_processes 2;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid文件的位置
pid nginx.pid;

events {
worker_connections 10240;
}

http {

include       mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

    #   open(OUTFILE, ">>/home/wamdm/perl_learn/a");
    #   print OUTFILE ($r->uri,"\n");
    #   close (OUTFILE);


perl_set $fix_upper_lower_case '
    use File::Basename;
    sub {
        my $r = shift;
        my $uri = $r->uri;
        my $filepath = $r->filename; 
        my $uri_prefix = substr($uri, 0, rindex($uri, "/") + 1);
        my $dir = dirname($filepath);
        my $filename = basename($filepath);
        opendir(my $dh, $dir) || die ("~~fail to open dir $dir");
        my @files = grep { /$filename/i && -f "$dir/$_" } readdir($dh);
        closedir($dh);
        if (@files > 0) {
            return "$uri_prefix@files[0]";
        }
        return $r->uri;
    }   
';  

server {

       if ($http_user_agent ~* "MJ12bot|qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|YandexBot|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") 
        { 
            return 403; 

        }    

listen 80;

# server_name localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

# location / {
# root html;
# index index.html index.htm;
# }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #

# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root html;
# }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443;
#    server_name  localhost;

#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers   on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

}

站点的conf文件如下:
server {
listen 80;
server_name computer.cdblp.cn;
access_log /home/wamdm/sites/logs/computer.access.log main;
error_log /home/wamdm/sites/logs/computer.error.log error;

root /home/wamdm/sites/searchscholar/computer;
index index.php index.html index.htm;

rewrite  "^/conference/([^/]+)$" /con_detail.php?con_title=$1 last;
rewrite  "^/conference/([^/]+)/$" /con_detail.php?con_title=$1 last;

if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") {

return 403;

}

#大小转换的补丁,处理从windows平台(大小写不敏感)迁移到ubuntu(大小写敏感)的站点
#对于需要url重写生效的请求失效
#if ( !-e $request_filename ) {
#   rewrite ^(.*)$ $fix_upper_lower_case last;
#}

#location /{
 #   include agent_deny.conf;

# }

#favicon.ico不用打日志
location = /favicon.ico {
    log_not_found off;
    access_log off;
}

#不允许访问隐藏文件
location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
}

#访问图片,flash文件等不用打日志
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
    expires      7d; #文件返回的过期时间是7天
    access_log off;
}

#访问js和css文件不用打日志
location ~ .*\.(js|css)?$ {
    expires      1d; #文件返回的过期时间是1天
    access_log off;
}


#设置php-cgi
location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    #拦截不存在的php页面请求
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }

}

}

  • 写回答

7条回答

  • Go 旅城通票 2014-12-14 09:33
    关注

    配置robots.txt禁止爬虫来爬就好了吧。。不过要是碰到流氓爬虫不理会robots.txt的配置,谷歌百度搜狗这种大部分是遵守的

    如何使用robots.txt及其详解

     User-agent: *
    Disallow: /
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?