参考资料

  1. nginx是否需要开启HTTP/2支持以提高安全性?
  2. nginx 配置静态文件路径
  3. Nginx upstream容错机制详解详细说明以及案例
  4. Nginx Stream(TCP/UDP)负载均衡详细说明以及案例
  5. Nginx静态资源服务器搭建详细说明以及案例
  6. Nginxrewrite重定向配置详解
  7. Nginx负载均衡策略详细说明以及案例
  8. 如何防止SQL注入和XSS攻击?
  1. 安装Nginx和WAF模块

    • 安装Nginx

    • 添加ModSecurity模块(如LibModSecurity)

  2. 配置WAF规则

    • 使用OWASP核心规则集(CRS)

    • 示例配置片段:

      modsecurity_rules_file /etc/nginx/modsec/main.conf;
      modsecurity on;
  3. Nginx安全加固配置

    • 禁用服务器令牌:

      server_tokens off;
    • 设置安全头部:

      add_header X-Frame-Options "SAMEORIGIN";
      add_header X-Content-Type-Options "nosniff";
  4. 限制请求处理

    • 限制请求大小:

      client_max_body_size 10m;
    • 限制请求方法:

      if ($request_method !~ ^(GET|POST|HEAD)$) {
          return 444;
      }
  5. 日志监控

    • 配置ModSecurity审计日志:

      SecAuditLog /var/log/nginx/modsec_audit.log
  6. 示例完整配置

    server {
        listen 80;
        modsecurity on;
        modsecurity_rules_file /etc/nginx/modsec/main.conf;
        
        location / {
            proxy_pass http://backend;
            modsecurity_rules_file /etc/nginx/modsec/crs-setup.conf;
            modsecurity_rules_file /etc/nginx/modsec/rules/*.conf;
        }
    }