国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

Nginx服務(wù)器屏蔽與禁止屏蔽網(wǎng)絡(luò)爬蟲的方法

2024-08-30 12:24:29
字體:
供稿:網(wǎng)友

每個網(wǎng)站通常都會遇到很多非搜索引擎的爬蟲,這些爬蟲大部分都是用于內(nèi)容采集或是初學(xué)者所寫,它們和搜索引擎的爬蟲不一樣,沒有頻率控制,往往會消耗大量服務(wù)器資源,導(dǎo)致帶寬白白浪費了。

其實Nginx可以非常容易地根據(jù)User-Agent過濾請求,我們只需要在需要URL入口位置通過一個簡單的正則表達(dá)式就可以過濾不符合要求的爬蟲請求:

location / {  if ($http_user_agent ~* "python|curl|java|wget|httpclient|okhttp") {    return 503;  }  # 其它正常配置  ...}

注意:變量$http_user_agent是一個可以直接在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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對錯新站長站的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 五指山市| 武鸣县| 札达县| 阿巴嘎旗| 涞水县| 元氏县| 光山县| 龙泉市| 密云县| 河南省| 舞钢市| 嘉义市| 阿坝| 乌海市| 林西县| 大邑县| 邻水| 泸州市| 建平县| 呼玛县| 铜鼓县| 沧州市| 永平县| 东海县| 营山县| 清原| 沙河市| 宁乡县| 武清区| 托克托县| 来凤县| 昌都县| 那曲县| 枝江市| 青冈县| 长寿区| 岢岚县| 康马县| 和田县| 承德县| 大安市|