Nginx设置防爬虫策略
生活随笔
收集整理的這篇文章主要介紹了
Nginx设置防爬虫策略
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有助于網站的爬蟲可以提升網站排名,比如百度蜘蛛。但有些爬蟲對服務器惡意獲取網站信息,不遵守robots規則,我們需要進行攔截??梢越鼓承︰ser Agent抓取網站。
- 新建配置配置文件
(例如進入到nginx安裝目錄下的conf目錄,創建: agent_deny.conf)
#禁止Scrapy等工具的抓取 if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {return 403; }#禁止指定UA及UA為空的訪問 if ($http_user_agent ~ "WinHttp|WebZIP|FetchURL|node-superagent|java/| FeedDemon|Jullo|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot| CrawlDaddy|Java|Feedly|Apache-HttpAsyncClient|UniversalFeedParser|ApacheBench| Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib| lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|BOT/0.1| YandexBot|FlightDeckReports|Linguee Bot|^$" ) {return 403; }- 然后,在網站相關配置中的 server段插入如下代碼:
- 重啟nginx:
- 測試
使用curl -A 模擬抓取即可,比如:
curl -I -A 'YYSpider' www.haoeasy.cn結果
[root@izwz93bcx7adgtozg4rvanz conf]# curl -I -A 'YYSpider' www.haoeasy.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.haoeasy.cn結果
[root@izwz93bcx7adgtozg4rvanz conf]# curl -I -A' ' www.haoeasy.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模擬百度蜘蛛的抓取:
curl -I -A 'Baiduspider' www.haoeasy.cn [root@izwz93bcx7adgtozg4rvanz conf]# curl -I -A 'Baiduspider' www.haoeasy.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類型
總結
以上是生活随笔為你收集整理的Nginx设置防爬虫策略的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eclipse插件(RCP)项目打包
- 下一篇: Nginx —— 检查配置文件ngi