插件窝 干货文章 Nginx访问限制配置,防止恶意访问和爬虫攻击

Nginx访问限制配置,防止恶意访问和爬虫攻击

limit Nginx 配置 zone 964    来源:    2025-04-17

Nginx访问限制配置指南

1. 基础访问限制

1.1 限制请求速率

http {
    limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;

    server {
        location / {
            limit_req zone=req_limit burst=20 nodelay;
            # 其他配置...
        }
    }
}
  • 10r/s表示每秒10个请求
  • burst=20允许突发20个请求
  • nodelay表示不延迟处理突发请求

1.2 限制并发连接数

http {
    limit_conn_zone $binary_remote_addr zone=conn_limit:10m;

    server {
        location / {
            limit_conn conn_limit 5;
            # 其他配置...
        }
    }
}
  • 限制单个IP最多5个并发连接

2. 防止爬虫攻击

2.1 屏蔽常见恶意User-Agent

server {
    if ($http_user_agent ~* (Scrapy|Curl|HttpClient|python-requests|wget|WinHTTP|X11|AhrefsBot|MJ12bot|SemrushBot|DotBot|Baiduspider|YandexBot|Sogou|Exabot|CCBot|PetalBot)) {
        return 403;
    }
    # 其他配置...
}

2.2 屏蔽特定IP或IP段

location / {
    deny 123.123.123.123;
    deny 123.123.123.0/24;
    allow all;
    # 其他配置...
}

3. 高级防护配置

3.1 限制特定URI的访问频率

http {
    limit_req_zone $binary_remote_addr zone=login_limit:10m rate=2r/m;

    server {
        location = /login {
            limit_req zone=login_limit burst=3 nodelay;
            # 登录处理逻辑...
        }
    }
}
  • 对登录页面限制为每分钟2次请求

3.2 动态黑名单

http {
    geo $blacklist {
        default 0;
        include /etc/nginx/conf.d/blacklist.conf;
    }

    server {
        if ($blacklist) {
            return 403;
        }
        # 其他配置...
    }
}
  • /etc/nginx/conf.d/blacklist.conf中维护黑名单IP

3.3 验证码保护

location /api/ {
    # 检查请求是否包含有效token
    if ($http_x_captcha_token != "your-secret-token") {
        return 403;
    }
    # 其他配置...
}

4. 日志记录与分析

4.1 记录被拒绝的请求

log_format blocked '$time_local|$remote_addr|$http_user_agent|$request';

server {
    location / {
        access_log /var/log/nginx/blocked.log blocked if=$limit_req_status;
        # 其他配置...
    }
}

5. 综合示例配置

http {
    # 定义限制区域
    limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
    limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
    limit_req_zone $binary_remote_addr zone=login_limit:10m rate=2r/m;

    # 加载黑名单
    geo $blacklist {
        default 0;
        include /etc/nginx/conf.d/blacklist.conf;
    }

    server {
        # 基础防护
        if ($blacklist) {
            return 403;
        }

        if ($http_user_agent ~* (Scrapy|Curl|HttpClient|python-requests)) {
            return 403;
        }

        location / {
            limit_req zone=req_limit burst=20 nodelay;
            limit_conn conn_limit 5;
            # 其他配置...
        }

        location = /login {
            limit_req zone=login_limit burst=3 nodelay;
            # 登录处理逻辑...
        }

        # 记录被阻止的请求
        access_log /var/log/nginx/blocked.log blocked if=$limit_req_status;
    }
}

注意事项

  1. 配置变更后记得测试:nginx -t 检查语法,然后 nginx -s reload 重新加载配置
  2. 根据实际业务需求调整限制阈值,避免误伤正常用户
  3. 对于API接口,考虑使用更专业的WAF(Web应用防火墙)解决方案
  4. 定期分析访问日志,更新防护策略

这些配置可以有效防止大多数恶意访问和简单爬虫攻击,但对于高级DDoS攻击或专业爬虫,可能需要结合云防护服务或专业安全解决方案。