当前位置:淮安SEO > 网站技巧 >

Nginx蜘蛛访问网站的方法

发布时间:2019-02-16 12:02:06  来源:互联网  作者:淮安SEOwww.huaianseo.cn  点这评论

对于做国内站的我来说,我不希望国外蜘蛛来访问我的网站,特别是个别垃圾蜘蛛,它们访问特别频繁。这些垃圾流量多了之后,严重浪费服务器的带宽和资源。通过判断user agent,在nginx中禁用这些蜘蛛可以节省一些流量,也可以防止一些恶意的访问。

步骤

1、进入nginx的配置目录,例如cd /usr/local/nginx/conf

2、添加agent_deny.conf配置文件

#禁止Scrapy等工具的抓取
if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
  return 403;
}
#禁止指定UA及UA为空的访问
if ($http_user_agent ~ "FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|LinkpadBot|Ezooms|^$" )
{
  return 403;
}
#禁止非GET|HEAD|POST方式的抓取
if ($request_method !~ ^(GET|HEAD|POST)$) {
  return 403;
}

3、在网站相关配置文件中插入代码“include agent_deny.conf ;”。

location ~ [^/]\.php(/|$)
{
  try_files $uri =404;
  fastcgi_pass  unix:/tmp/php-cgi.sock;
  fastcgi_index index.php;
  include fastcgi.conf;
  include agent_deny.conf ;
}

4、重新加载nginx

/etc/init.d/nginx reload

测试

通过curl模拟蜘蛛抓取访问。

[email protected]:~# curl -I -A "BaiduSpider" www.sijitao.net
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 09 Feb 2015 03:37:20 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.5.19
Vary: Accept-Encoding, Cookie
Cache-Control: max-age=3, must-revalidate
WP-Super-Cache: Served supercache file from PHP

[email protected]199:~# curl -I -A "JikeSpider" www.sijitao.net           
HTTP/1.1 403 Forbidden
Server: nginx
Date: Mon, 09 Feb 2015 03:37:44 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive

[email protected]199:~# curl -I -A "" www.sijitao.net             
HTTP/1.1 403 Forbidden
Server: nginx
Date: Mon, 09 Feb 2015 03:37:52 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive

nginx日志上的效果如下。

相关文章

评论赞助本站

test