對於做國內站的我來說,我不希望國外蜘蛛來訪問我的網站,特別是個別垃圾蜘蛛,它們訪問特別頻繁。這些垃圾流量多了之后,嚴重浪費服務器的帶寬和資源。通過判斷user agent,在nginx中禁用這些蜘蛛可以節省一些流量,也可以防止一些惡意的訪問。
方法一:修改nginx.conf,禁止網絡爬蟲的user_agent,返回403。
1、進入nginx的配置目錄,例如cd /usr/local/nginx/conf
2、添加agent_deny.conf配置文件 vim agent_deny.conf
server層加入以下內容:
#禁止Scrapy等爬蟲工具的抓取 if ($http_user_agent ~* "Scrapy|Sogou web spider|Baiduspider") { return 403; } #禁止指定UA及UA為空的訪問 if ($http_user_agent ~ "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|LinkpadBot|Ezooms|^$" ) { return 403; } #禁止非GET|HEAD|POST方式的抓取 if ($request_method !~ ^(GET|HEAD|POST)$) { return 403; }
還有加一些針對特殊的user_agent的訪問
if ($http_user_agent ~ "Mozilla/4.0\ \(compatible;\ MSIE\ 6.0;\ Windows\ NT\ 5.1;\ SV1;\ .NET\ CLR\ 1.1.4322;\ .NET\ CLR\ 2.0.50727\)") {
return 404;
}
然后測試一下 設置是否成功,curl的-A 可以讓我們隨意指定自己這次訪問所宣稱的自己的瀏覽器信息
#curl -I -A "BaiduSpider" www.test.com (模擬瀏覽器頭信息)
HTTP/1.1 200 OK Server: nginx Date: Mon, 09 Feb 2015 03:37:20 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Vary: Accept-Encoding X-Powered-By: PHP/5.5.19 Vary: Accept-Encoding, Cookie Cache-Control: max-age=3, must-revalidate WP-Super-Cache: Served supercache file from PHP
#curl -I -A "JikeSpider" www.test.com
HTTP/1.1 403 Forbidden Server: nginx Date: Mon, 09 Feb 2015 03:37:44 GMT Content-Type: text/html Content-Length: 162 Connection: keep-alive
到這里,nginx通過判斷User-Agent屏蔽蜘蛛訪問網站就已經完成,可以根據實際情況對agent_deny.conf中的蜘蛛進行增加、刪除或者修改。
方法2:網站更目錄下增加Robots.txt,放在站點根目錄下。
在http://tool.chinaz.com/robots/站點可以針對現在的搜索引擎按照想要的規則生成robots.txt文件。
知識擴展:
robots.txt是搜索引擎中訪問網站的時候要查看的第一個文件。robots.txt文件告訴蜘蛛程序在服務器上什么文件是可以被查看的。
-----------------------------------------------------------------------------------------------------------------------------
Nginx也可實現根據訪問源的設備類型進行判斷並跳轉到不同的tomcat或其它項目中
vim /usr/local/nginx/conf/conf.d/mobile.conf
upstream mobileserver { server 10.0.10.48:8089 max_fails=3 fail_timeout=60 weight=1; server 10.0.10.49:8089 max_fails=3 fail_timeout=60 weight=1; server 10.0.10.50:8089 max_fails=3 fail_timeout=60 weight=1; } upstream computerserver { server 10.0.10.48:8080 max_fails=3 fail_timeout=60 weight=1; server 10.0.10.49:8080 max_fails=3 fail_timeout=60 weight=1; server 10.0.10.50:8080 max_fails=3 fail_timeout=60 weight=1; } server { listen 80; server_name house.wjoyxt.com; rewrite_log on; if ($request_uri ~ " ") { return 444; } location / {
#以下兩行為重新定義或者添加發往后端服務器的請求頭(在使用反向代理時經常用)
proxy_set_header Host $host; #如果不想改變請求頭“Host”的值,可以這樣來設置:proxy_set_header Host $http_host;但是,如果客戶端請求頭中沒有攜帶這個頭部,那么傳遞到后端服務器的請求也不含這個頭部。這種情況下,更好的方式是使用$host變量——它的值在請求包含“Host”請求頭時為“Host”字段的值,在請求未攜帶“Host”請求頭時為虛擬主機的主域名 proxy_set_header X-Real-IP $remote_addr; #把真實的客戶端ip發送給后端的web服務器 access_log /data/logs/nginx/mobile.access.log main; error_log /data/logs/nginx/mobile.error.log; set $client ""; #如果是IPhone設備、iPad設備、iPod設備、蘋果其它非PC設備、蘋果PC設備 if ( $http_user_agent ~* "(iPhone|iPad|iPod|iOS|Android|Mobile|nokia|samsung|htc|blackberry)") { set $client "1"; } if ($client = '1') { proxy_pass http://mobileserver; break; } if (!-f $request_filename) { proxy_pass http://computerserver; break; } } location ~ \.php$ { proxy_pass http://127.0.0.1; } location ~* \.(html|shtml|htm|inc|log)$ { expires 1m; } location ~* ^.+\.(jpg|jpeg|gif|swf|mpeg|mpg|mov|flv|asf|wmv|avi|ico)$ { expires 15d; } }