nginx 防盗链防爬虫配置详解
新建配置配置文件(例如进入到nginx安装目录下的conf目录,创建:agent_deny.conf)
禁止Scrapy等工具的抓取if($http_user_agent~*(Scrapy|Curl|HttpClient)){return403;}
禁止指定UA及UA为空的访问
#forbiddenScrapy if($http_user_agent~*(Scrapy|Curl|HttpClient)) { return403; } #forbiddenUA if($http_user_agent~"Bytespider|FeedDemon|JikeSpider|IndyLibrary|AlexaToolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|MicrosoftURLControl|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReportsBot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$") { return403; } #forbiddennotGET|HEAD|POSTmethodaccess if($request_method!~^(GET|HEAD|POST)$) { return403; }
然后,在网站相关配置中的server段插入如下代码:includeagent_deny.conf;
重启nginx:
/data/nginx/sbin/nginx-sreload
测试使用curl-A模拟抓取即可,比如:
curl-I-A'YYSpider'<>
结果
[root@11conf]#curl-I-A'YYSpider'www.xxx.cn
HTTP/1.1403Forbidden
Server:nginx/1.12.0
Date:Wed,24Apr201911:35:21GMT
Content-Type:text/html
Content-Length:169
Connection:keep-alive
模拟UA为空的抓取:
curl-I-A''<>
结果
[root@11conf]#curl-I-A''www.xxx.cn
HTTP/1.1403Forbidden
Server:nginx/1.12.0
Date:Wed,24Apr201911:36:06GMT
Content-Type:text/html
Content-Length:169
Connection:keep-alive
模拟百度蜘蛛的抓取:
curl-I-A'Baiduspider'<<>>
[root@11conf]#curl-I-A'Baiduspider'www.xxx.cn
HTTP/1.1200OK
Server:nginx/1.12.0
Date:Wed,24Apr201911:36:47GMT
Content-Type:text/html
Content-Length:612
Last-Modified:Fri,12Apr201913:49:36GMT
Connection:keep-alive
ETag:"5cb09770-264"
Accept-Ranges:bytes
UA类型
FeedDemon内容采集 BOT/0.1(BOTforJCE)sql注入 CrawlDaddysql注入 Java内容采集 Jullo内容采集 Feedly内容采集 UniversalFeedParser内容采集 ApacheBenchcc攻击器 Swiftbot无用爬虫 YandexBot无用爬虫 AhrefsBot无用爬虫 YisouSpider无用爬虫(已被UC神马搜索收购,此蜘蛛可以放开!) jikeSpider无用爬虫 MJ12bot无用爬虫 ZmEuphpmyadmin漏洞扫描 WinHttp采集cc攻击 EasouSpider无用爬虫 HttpClienttcp攻击 MicrosoftURLControl扫描 YYSpider无用爬虫 jauntywordpress爆破扫描器 oBot无用爬虫 Python-urllib内容采集 IndyLibrary扫描 FlightDeckReportsBot无用爬虫 LingueeBot无用爬虫
nginx防盗链配置
背景:防止第三方引用链接访问我们的图片,消耗服务器资源和网络流量,我们可以在服务器上做防盗链限制。
实现防盗链的方式有两种:refer方式和签名方式。
refer方式实现防盗链
工作模块:ngx_http_referer_module。
作用变量:$invalid_referer,全局变量。
配置域:server,location
配置:
server{ listen80; server_namewww.imcati.comrefer-test.imcati.com; root/usr/share/nginx/html; location~*\.(gif|jpg|jpeg|png|bmp|swf)${ valid_referersnoneblockedwww.imcati.com; if($invalid_referer){ return403; } } }
- valid_referers:指定资源访问是通过以下几种方式为合法,即白名单。vaild_referers有效的引用连接,如下,否则就进入$invaild_refere,返回403forbiden。
- none:允许缺失的头部访问。
- blocked:允许referer没有对应值的请求。
- server_names:若referer站点域名与server_name中本机配的域名一样允许访问。
到此这篇关于nginx防盗链防爬虫配置详解的文章就介绍到这了,更多相关nginx防盗链防爬虫配置内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!