以下环境是使用军哥LNMP的一键脚本(环境不同自行调整)

进入到nginx安装目录下的conf目录,将如下代码保存为agent_deny.conf

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

禁止Scrapy等工具的抓取

if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
     return 403;
}

禁止指定UA及UA为空的访问

if ($http_user_agent ~ " PHP|FeedDemon|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|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$" ) {
     return 403;
}

禁止非GET|HEAD|POST方式的抓取

if ($request_method !~ ^(GET|HEAD|POST)$) {
    return 403;
}

以上三段代码去掉介绍编辑入agent_deny.conf

然后,在网站相关配置中的location / { 之后插入如下代码:

include agent_deny.conf;

如下配置:

vi /usr/local/nginx/conf/vhost/xxx.com.conf
location / {
        try_files $uri $uri/ /index.php?$args;
        #这个位置新增1行:
        include agent_deny.conf;
        rewrite ^/sitemap_360_sp.txt$ /sitemap_360_sp.php last;

保存后,执行如下命令,平滑重启nginx即可:

lnmp nginx reload
Chinese Stock News 7x24
Last modification:September 3, 2019
如果觉得我的文章对你有用,请随意赞赏