插件窝 干货文章 Nginx单机与负载均衡的反向代理安全

Nginx单机与负载均衡的反向代理安全

Nginx limit 安全 限制 961    来源:    2025-04-20

Nginx反向代理安全配置:单机与负载均衡环境

单机Nginx反向代理安全配置

1. 基础安全加固

# 隐藏Nginx版本信息
server_tokens off;

# 限制HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
    return 405;
}

# 禁用不必要的HTTP头
more_clear_headers 'Server';
more_clear_headers 'X-Powered-By';

2. SSL/TLS安全配置

ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;

3. 请求限制与防护

# 限制请求体大小
client_max_body_size 10m;

# 限制缓冲区大小
client_body_buffer_size 128k;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;

# 限制连接速率
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;

server {
    limit_req zone=req_limit burst=20 nodelay;
}

4. 访问控制

# IP黑白名单
allow 192.168.1.0/24;
deny all;

# 基于地理位置限制
geo $blocked_country {
    default 0;
    include /etc/nginx/conf.d/geo_block.conf;
}

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

负载均衡环境安全配置

1. 负载均衡器基础配置

upstream backend {
    # 使用最少连接算法
    least_conn;

    # 配置健康检查
    server backend1.example.com max_fails=3 fail_timeout=30s;
    server backend2.example.com max_fails=3 fail_timeout=30s;
    server backend3.example.com max_fails=3 fail_timeout=30s;

    # 保持连接数
    keepalive 32;
}

2. 后端服务器安全通信

# 使用SSL与后端通信
proxy_ssl_verify on;
proxy_ssl_trusted_certificate /path/to/trusted_ca.crt;
proxy_ssl_verify_depth 2;
proxy_ssl_session_reuse on;

# 设置适当的超时
proxy_connect_timeout 5s;
proxy_send_timeout 10s;
proxy_read_timeout 30s;

3. DDoS防护

# 限制连接数
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;

server {
    limit_conn conn_limit 20;
}

# 速率限制特定URL
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=100r/m;

location /api/ {
    limit_req zone=api_limit burst=200 nodelay;
}

4. Web应用防火墙集成

# ModSecurity集成示例
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;

# 或者使用NAXSI
location / {
    # 启用NAXSI核心规则
    SecRulesEnabled;

    # 学习模式(生产环境应关闭)
    LearningMode;

    # 拒绝规则
    DeniedUrl "/RequestDenied";

    # 检查规则
    CheckRule "$SQL >= 8" BLOCK;
    CheckRule "$RFI >= 8" BLOCK;
    CheckRule "$TRAVERSAL >= 4" BLOCK;
    CheckRule "$EVADE >= 4" BLOCK;
    CheckRule "$XSS >= 8" BLOCK;
}

通用安全最佳实践

  1. 定期更新:保持Nginx和所有模块更新到最新稳定版本

  2. 最小权限原则

    chown -R nginx:nginx /var/log/nginx
    chmod -R 750 /var/log/nginx
    
  3. 日志监控

    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;
    
  4. 文件权限限制

    location ~* \.(htaccess|htpasswd|ini|log|sh|cfg|inc|bak|swp)$ {
       deny all;
    }
    
    location ~ /\. {
       deny all;
    }
    
  5. HTTP安全头

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
    add_header X-XSS-Protection "1; mode=block";
    add_header Content-Security-Policy "default-src 'self';";
    add_header Referrer-Policy "strict-origin-when-cross-origin";
    
  6. 定期审计

    • 使用nginx -t测试配置
    • 使用工具如Lynis进行安全审计
    • 监控异常访问模式

通过实施这些安全措施,无论是单机还是负载均衡环境下的Nginx反向代理,都能显著提高系统的安全性,抵御常见的网络攻击。