最近我发现一些人正试图镜像我的网站。 他们通过两种方式来做到这一点:
假装是谷歌蜘蛛。 访问日志如下:
89.85.93.235 - - [05/May/2015:20:23:16 +0800] "GET /robots.txt HTTP/1.0" 444 0 "http://www.example.com" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "66.249.79.138" 79.85.93.235 - - [05/May/2015:20:23:34 +0800] "GET /robots.txt HTTP/1.0" 444 0 "http://www.example.com" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "66.249.79.154"
http_x_forwarded_for地址是google地址。
假装是一个正常的网页浏览器。
我试图使用下面的configuration来阻止他们的访问:
对于问题1,我将检查X_forward_for地址。 如果用户代理是蜘蛛,并且X_forward_for不为空。 然后阻止。 我在用着
map $http_x_forwarded_for $xf { default 1; "" 0; } map $http_user_agent $fakebots { default 0; "~*bot" $xf; "~*bing" $xf; "~*search" $xf; } if ($fakebots) { return 444; }
有了这个configuration,似乎假的谷歌蜘蛛无法访问我的网站的根。 但他们仍然可以访问我的PHP文件,他们不能访问和JS或CSS文件。 很奇怪。 我不知道什么是错的。
对于声明它们不是蜘蛛的问题2用户代理。 我将使用ngx_lua生成一个随机值并将其添加到cookie中,然后检查是否可以将该值发送回去。 如果他们不能发回,那就意味着他们是机器人和块访问。
map $http_user_agent $ifbot { default 0; "~*Yahoo" 1; "~*archive" 1; "~*search" 1; "~*Googlebot" 1; "~Mediapartners-Google" 1; "~*bingbot" 1; "~*msn" 1; "~*rogerbot" 3; "~*ChinasoSpider" 3; } if ($ifbot = "0") { set $humanfilter 1; } #below section is to exclude flash upload if ( $request_uri !~ "~mod\=swfupload\&action\=swfupload" ) { set $humanfilter "${humanfilter}1"; } if ($humanfilter = "11") { rewrite_by_lua ' local random = ngx.var.cookie_random if(random == nil) then random = math.random(999999) end local token = ngx.md5("hello" .. ngx.var.remote_addr .. random) if (ngx.var.cookie_token ~= token) then ngx.header["Set-Cookie"] = {"token=" .. token, "random=" .. random} return ngx.redirect(ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.request_uri) end '; }
但似乎以上configuration,谷歌机器人也被封锁,而不应该。
最后的问题是我试图用“拒绝”来拒绝来自IP的访问。 但它似乎仍然可以访问我的服务器。
在我的http部分:
deny 69.85.92.0/23; deny 69.85.93.235;
但是,当我检查日志,我仍然可以find
69.85.93.235 - - [05/May/2015:19:44:22 +0800] "GET /thread-1251687-1-1.html HTTP/1.0" 302 154 "http://www.example.com" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" "123.125.71.107" 69.85.93.235 - - [05/May/2015:19:50:06 +0800] "GET /thread-1072432-1-1.html HTTP/1.0" 302 154 "http://www.example.com" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" "220.181.108.151" 69.85.93.235 - - [05/May/2015:20:15:44 +0800] "GET /archiver/tid-1158637.html?page=1 HTTP/1.0" 302 154 "http://www.example.com" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" "180.76.5.196" 69.85.93.235 - - [05/May/2015:20:45:09 +0800] "GET /forum.php?mod=viewthread&tid=1131476 HTTP/1.0" 302 154 "http://www.example.com" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" "123.125.71.53"
任何人都可以帮忙?