遇到疯狂蜘蛛爬网站,拉崩服务器;可以通过下面的限速方式,
【第一步】在主 nginx.conf 文件的 http {} 内加上下面代码(适用于 Cloudflare 用户):常见地址:/www/server/nginx/conf/nginx.conf
limit_req_zone $realip_remote_addr zone=html_limit:10m rate=2r/s;
limit_req_zone $realip_remote_addr zone=php_limit:10m rate=2r/s;
如果是普通直接解析的服务器就用这个:
limit_req_zone $binary_remote_addr zone=req_limit_perip:10m rate=2r/s;
注意,如果域名使用了 CDN,请改成:
limit_req_zone $http_x_forwarded_for zone=req_limit_perip:10m rate=2r/s;
注意:在http {} 内哦
【第二步】在单个网站-配置文件 server{}内加上下面 (html与php都限):
建议放在 include enable-php.conf;后面
———————————————————–
#php 页面限速,注意在主nginx.conf里http {} 加上
# limit_req_zone $realip_remote_addr zone=php_limit:10m rate=2r/s;
location ~ \.php$ {
try_files $uri =404;
limit_req zone=php_limit burst=10 nodelay;
limit_req_status 429;
include fastcgi_params;
fastcgi_pass unix:/tmp/php-cgi.sock; #有独立sock,替换成你实际的sock
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# HTML页面限速,注意在主nginx.conf里http {} 加上
# limit_req_zone $realip_remote_addr zone=html_limit:10m rate=2r/s;
location ~ \.html$ {
limit_req zone=html_limit burst=10 nodelay;
limit_req_status 429; #429 是标准限流,爬虫会“避让”SEO 友好(搜索引擎能识别)
try_files $uri $uri/ /index.php?$args;
}
# 禁止目录浏览(附加)
autoindex off;
【第三步】nginx.conf并同时开启:(或建议 直接用Cloudflare IP 段组也可以)
set_real_ip_from 0.0.0.0/0; # 或 Cloudflare IP 段
real_ip_header X-Forwarded-For;
下面是Cloudflare IP 段组
# Cloudflare IPv4 节点
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
# Cloudflare IPv6 节点
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;
# 设置真实 IP 获取来源头部
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
注意放在主nginx.conf 的 http {} 块内,重启Nginx
地址:/www/server/nginx/conf/nginx.conf