nginx 防盜鏈防爬蟲(chóng)配置詳解
新建配置配置文件 (例如進(jìn)入到nginx安裝目錄下的conf目錄,創(chuàng)建: agent_deny.conf)
禁止Scrapy等工具的抓取 if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) { return 403; }
禁止指定UA及UA為空的訪問(wèn)
#forbidden Scrapy
if ($http_user_agent ~* (Scrapy|Curl|HttpClient))
{
return 403;
}
#forbidden UA
if ($http_user_agent ~ "Bytespider|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|Ezooms|^$" )
{
return 403;
}
#forbidden not GET|HEAD|POST method access
if ($request_method !~ ^(GET|HEAD|POST)$)
{
return 403;
}
然后,在網(wǎng)站相關(guān)配置中的 server段插入如下代碼: include agent_deny.conf;
重啟nginx:
/data/nginx/sbin/nginx -s reload
測(cè)試 使用curl -A 模擬抓取即可,比如:
curl -I -A 'YYSpider' <<www.xxx.con>>
結(jié)果
[root@11 conf]# curl -I -A 'YYSpider' www.xxx.cn
HTTP/1.1 403 Forbidden
Server: nginx/1.12.0
Date: Wed, 24 Apr 2019 11:35:21 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
模擬UA為空的抓取:
curl -I -A' ' <<www.xxx.cn>>
結(jié)果
[root@11 conf]# curl -I -A' ' www.xxx.cn
HTTP/1.1 403 Forbidden
Server: nginx/1.12.0
Date: Wed, 24 Apr 2019 11:36:06 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
模擬百度蜘蛛的抓?。?br/>
curl -I -A 'Baiduspider' <<<www.xxx.cn>>>
[root@11 conf]# curl -I -A 'Baiduspider' www.xxx.cn
HTTP/1.1 200 OK
Server: nginx/1.12.0
Date: Wed, 24 Apr 2019 11:36:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 12 Apr 2019 13:49:36 GMT
Connection: keep-alive
ETag: "5cb09770-264"
Accept-Ranges: bytes
UA類型
FeedDemon 內(nèi)容采集 BOT/0.1 (BOT for JCE) sql注入 CrawlDaddy sql注入 Java 內(nèi)容采集 Jullo 內(nèi)容采集 Feedly 內(nèi)容采集 UniversalFeedParser 內(nèi)容采集 ApacheBench cc攻擊器 Swiftbot 無(wú)用爬蟲(chóng) YandexBot 無(wú)用爬蟲(chóng) AhrefsBot 無(wú)用爬蟲(chóng) YisouSpider 無(wú)用爬蟲(chóng)(已被UC神馬搜索收購(gòu),此蜘蛛可以放開(kāi)?。?jikeSpider 無(wú)用爬蟲(chóng) MJ12bot 無(wú)用爬蟲(chóng) ZmEu phpmyadmin 漏洞掃描 WinHttp 采集cc攻擊 EasouSpider 無(wú)用爬蟲(chóng) HttpClient tcp攻擊 Microsoft URL Control 掃描 YYSpider 無(wú)用爬蟲(chóng) jaunty wordpress爆破掃描器 oBot 無(wú)用爬蟲(chóng) Python-urllib 內(nèi)容采集 Indy Library 掃描 FlightDeckReports Bot 無(wú)用爬蟲(chóng) Linguee Bot 無(wú)用爬蟲(chóng)
nginx 防盜鏈配置
背景:防止第三方引用鏈接訪問(wèn)我們的圖片,消耗服務(wù)器資源和網(wǎng)絡(luò)流量,我們可以在服務(wù)器上做防盜鏈限制。
實(shí)現(xiàn)防盜鏈的方式有兩種:refer方式和簽名方式。
refer方式實(shí)現(xiàn)防盜鏈
工作模塊:ngx_http_referer_module。
作用變量:$invalid_referer,全局變量。
配置域:server, location
配置:
server {
listen 80;
server_name www.imcati.com refer-test.imcati.com;
root /usr/share/nginx/html;
location ~*\.(gif|jpg|jpeg|png|bmp|swf)$ {
valid_referers none blocked www.imcati.com;
if ($invalid_referer) {
return 403;
}
}
}
- valid_referers: 指定資源訪問(wèn)是通過(guò)以下幾種方式為合法,即白名單。 vaild_referers 有效的引用連接,如下,否則就進(jìn)入$invaild_refere,返回403 forbiden。
- none:允許缺失的頭部訪問(wèn)。
- blocked:允許referer沒(méi)有對(duì)應(yīng)值的請(qǐng)求。
- server_names:若referer站點(diǎn)域名與server_name中本機(jī)配的域名一樣允許訪問(wèn)。
到此這篇關(guān)于nginx 防盜鏈防爬蟲(chóng)配置詳解的文章就介紹到這了,更多相關(guān)nginx 防盜鏈防爬蟲(chóng)配置內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信