用宝塔面板的网站,搭建把防爬虫及无效蜘蛛规则
建议你把防爬虫规则放在 /www/server/nginx/conf/blockbots.conf,宝塔的 Nginx 主配置通常在这个路径下加载额外配置。
一、打开路径: /www/server/nginx/conf/
在这个路径下新建个文件:blockbots.conf ,复制下面的代码进去保存。
————————————
# 通用防爬虫规则 if ($http_user_agent ~* "(Scrapy|Curl|HttpClient|AhrefsBot|MJ12bot|Bytespider|FeedDemon|JikeSpider|heritrix|MegaIndex|Sogou|Zoominfo|dotbot|python-requests|Wget|Java|^$)") { return 403; } if ($request_method !~ ^(GET|HEAD|POST)$) { return 403; } if ($http_user_agent = "") { return 403; }
------------------------------------
二、在每个站点配置中 include 引入防爬虫规则
打开宝塔面板 → 网站 → 选择你的站点(如 gushu6.com)→ 点击【设置】→进入【配置文件】
在 server {} 代码块中加一行:
------------------------------------
include /www/server/nginx/conf/blockbots.conf;
------------------------------------
示例:server { listen 443 ssl http2; server_name gushu.com www.gushu.com; include /www/server/nginx/conf/blockbots.conf; root /www/wwwroot/gushu6.com; index index.php index.html; ... }