插件窝 干货文章 如何使用Nginx实现限制各种恶意访问

如何使用Nginx实现限制各种恶意访问

Nginx limit location zone 489    来源:    2025-04-20

使用Nginx限制恶意访问的全面方案

Nginx作为高性能的Web服务器和反向代理,提供了多种机制来防御和限制恶意访问。以下是全面的解决方案:

1. 基础访问限制

1.1 限制连接频率

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

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

1.2 限制并发连接数

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

    server {
        location / {
            limit_conn addr 10;  # 每个IP最多10个并发连接
        }
    }
}

2. 高级防护措施

2.1 阻止常见恶意User-Agent

server {
    if ($http_user_agent ~* (wget|curl|libwww-perl|nikto|sqlmap|nmap|scan|bot|spider|crawler)) {
        return 403;
    }
}

2.2 阻止恶意扫描工具

server {
    location ~* \.(php|asp|aspx|jsp|pl|cgi|sh|bak|inc|sql|log|conf|config|env)$ {
        deny all;
        return 404;
    }
}

2.3 防止目录遍历

server {
    location ~* \.(?:\.htaccess|\.git|\.svn|\.env) {
        deny all;
    }
}

3. IP黑名单管理

3.1 静态IP黑名单

http {
    geo $blocked_ip {
        default 0;
        1.2.3.4 1;  # 恶意IP
        5.6.7.8 1;  # 另一个恶意IP
    }

    server {
        if ($blocked_ip) {
            return 403;
        }
    }
}

3.2 动态IP黑名单(结合fail2ban)

  1. 安装fail2ban
  2. 配置Nginx日志监控规则

4. 防止CC攻击

4.1 动态限速

map $http_cf_connecting_ip $limit {
    default "";
    1.2.3.4 $binary_remote_addr;  # 信任Cloudflare IP
}

limit_req_zone $limit zone=dynamic:10m rate=5r/s;

server {
    location / {
        limit_req zone=dynamic burst=10 nodelay;
    }
}

4.2 验证码挑战

location /login {
    # 超过阈值后重定向到验证码页面
    error_page 503 = @captcha;
    limit_req zone=login_limit;
    # 正常处理
}

location @captcha {
    # 返回验证码页面或重定向到验证服务
}

5. 防止SQL注入和XSS

server {
    set $block_sql_injections 0;
    set $block_xss 0;

    if ($query_string ~* "union.*select.*\(") {
        set $block_sql_injections 1;
    }

    if ($query_string ~* "<script.*>") {
        set $block_xss 1;
    }

    if ($block_sql_injections = 1) {
        return 403;
    }

    if ($block_xss = 1) {
        return 403;
    }
}

6. 防止大文件攻击

server {
    client_max_body_size 10m;  # 限制上传文件大小

    location /upload {
        # 进一步限制上传速度
        client_body_buffer_size 128k;
        client_body_temp_path /var/nginx/client_body_temp;
        client_body_in_file_only off;
        client_body_timeout 60s;
    }
}

7. 日志分析与监控

http {
    log_format security '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $body_bytes_sent '
                       '"$http_referer" "$http_user_agent" '
                       '$request_time $upstream_response_time '
                       '$http_x_forwarded_for';

    access_log /var/log/nginx/security.log security;
}

8. 综合配置示例

http {
    # 定义共享内存区
    limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
    limit_conn_zone $binary_remote_addr zone=conn_limit:10m;

    # IP黑名单
    geo $block {
        default 0;
        include /etc/nginx/blockips.conf;
    }

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

        # 限制请求速率
        limit_req zone=req_limit burst=20 nodelay;

        # 限制并发连接
        limit_conn conn_limit 10;

        # 防止扫描
        location ~* \.(php|asp|aspx|jsp|pl|cgi|sh|bak|inc|sql|log|conf|config|env)$ {
            deny all;
            return 404;
        }

        # 防止目录遍历
        location ~ /\. {
            deny all;
            return 404;
        }

        # 防止大文件攻击
        client_max_body_size 10m;
    }
}

最佳实践建议

  1. 分层防御:结合网络层、应用层多级防护
  2. 日志分析:定期分析Nginx日志识别攻击模式
  3. 动态调整:根据攻击情况动态调整防护策略
  4. CDN配合:使用Cloudflare等CDN提供额外防护
  5. 定期更新:保持Nginx版本更新以获取最新安全补丁

以上配置可根据实际业务需求进行调整,建议在实施前进行充分测试以确保不影响正常用户访问。