天天看点

php 防止恶意灌水,Nginx禁止恶意User Agent采集网站

禁用一些User Agent可以节省一些流量也可以防止一些恶意的访问,尤其是部份搜索引擎爬虫,例如我们的网站就是一个地方性站点,没有必要被一些国外的搜索引擎爬虫索引,都可以禁掉,具体操作如下:

php 防止恶意灌水,Nginx禁止恶意User Agent采集网站

1、编辑该文件agent_deny.con:

# vi /usr/local/nginx/conf/agent_deny.conf

2、增加以下内容(示例):

if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {

return 404;

}

#空agent

if ($http_user_agent ~ ^$) {

return 404;

}

#请求方式限制

if ($request_method !~ ^(GET|HEAD|POST)$) {

return 403;

}

3、编辑文件nginx.conf:

在server中引入agent_deny.conf

include agent_deny.conf;

有一些单词中间有空格,所以两边需要使用双引号,禁用了若干个搜索引擎爬虫,还有几个恶意灌水机等,可以分析日志根据情况屏蔽恶意的User Agent。

本文地址:http://www.it300.com/article-15358.html