這篇文章將為大家詳細(xì)講解有關(guān)Nginx服務(wù)器如何屏蔽與禁止屏蔽網(wǎng)絡(luò)爬蟲,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司專注于固安企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),電子商務(wù)商城網(wǎng)站建設(shè)。固安網(wǎng)站建設(shè)公司,為固安等地區(qū)提供建站服務(wù)。全流程定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
每個(gè)網(wǎng)站通常都會(huì)遇到很多非搜索引擎的爬蟲,這些爬蟲大部分都是用于內(nèi)容采集或是初學(xué)者所寫,它們和搜索引擎的爬蟲不一樣,沒有頻率控制,往往會(huì)消耗大量服務(wù)器資源,導(dǎo)致帶寬白白浪費(fèi)了。
其實(shí)Nginx可以非常容易地根據(jù)User-Agent過濾請(qǐng)求,我們只需要在需要URL入口位置通過一個(gè)簡單的正則表達(dá)式就可以過濾不符合要求的爬蟲請(qǐng)求:
location / { if ($http_user_agent ~* "python|curl|java|wget|httpclient|okhttp") { return 503; } # 其它正常配置 ... }
注意:變量$http_user_agent
是一個(gè)可以直接在location中引用的Nginx變量。~*表示不區(qū)分大小寫的正則匹配,通過python就可以過濾掉80%的Python爬蟲。
Nginx中禁止屏蔽網(wǎng)絡(luò)爬蟲
server { listen 80; server_name www.xxx.com; #charset koi8-r; #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") { return 403; } location ~ ^/(.*)$ { proxy_pass http://localhost:8080; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
可以用 curl 測試一下
curl -I -A "qihoobot" www.xxx.com
關(guān)于“Nginx服務(wù)器如何屏蔽與禁止屏蔽網(wǎng)絡(luò)爬蟲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
本文標(biāo)題:Nginx服務(wù)器如何屏蔽與禁止屏蔽網(wǎng)絡(luò)爬蟲
轉(zhuǎn)載來于:http://www.rwnh.cn/article48/pgchep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站建設(shè)、網(wǎng)站改版、Google、網(wǎng)站策劃、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)