首页 > 代码

Nginx_遇到疯狂蜘蛛爬网站,拉崩服务器

遇到疯狂蜘蛛爬网站,拉崩服务器;可以通过下面的限速方式,

【第一步】在主 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


  • 智能线路分流(海外走 Cloudflare,国内走阿里 CDN)
  • 实现“海外用 Cloudflare 加速 + 国内绕回阿里 CDN”的 智能线路分流(CN 国内走阿里,海外走 […]

  • Cloudflare CDN 会影响百度收录的主要原因
  • 1. IP 在境外,百度蜘蛛访问异常 Cloudflare 的边缘节点大多在 境外(香港/东京/新加坡); 百 […]

  • cloudflare_几个实用WAF安全规则_散装版
  • 安全-WAF规则 设置(免费版) 进入 Cloudflare 后台 选择站点 → Security → WAF […]

  • Nginx服务器限制蜘蛛爬虫\过度分页\阻止执行 uploads 里的 php
  • 【拒绝恶意蜘蛛爬虫】 /www/server/panel/vhost/nginx下找到如wenxian8.co […]

  • Nginx为每个网站单独创建独立的 PHP-FPM 池(pool)
  • 为每个网站单独创建独立的 PHP-FPM 池(pool),例如:wenxian8.com。这是中高级服务器性能 […]

  • 用了cloudeflare域名解析,如何在Nginx 批量 IP 屏蔽
  • 【Nginx 批量 IP 屏蔽】 注意,如果你用了cloudeflare域名解析,那就要注意先找到真实攻击者I […]