Nginx安全配置最佳实践(2026版)——抵御现代Web攻击的完整指南
Nginx安全配置最佳实践(2026版)——抵御现代Web攻击的完整指南
Nginx作为全球使用率最高的Web服务器之一,承载着大量关键业务。然而,默认配置下Nginx存在诸多安全隐患。本文从攻击者视角出发,提供一套可落地的Nginx安全加固方案。
一、隐藏版本号与服务器信息
攻击者通常会通过服务器版本号定位已知漏洞。关闭版本号泄露是安全第一步。
# 在 http 块中配置 server_tokens off; # 自定义错误页面,避免泄露nginx路径 error_page 404 /404.html; error_page 500 502 503 504 /50x.html;
验证效果:curl -I https://yourdomain.com 返回头不再包含nginx版本。
二、TLS/SSL 安全配置(A+评分)
2026年建议的最低TLS标准:
ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; # 推荐使用自动OCSP装订 ssl_stapling on; ssl_stapling_verify on; resolver 8.8.8.8 1.1.1.1 valid=300s;
配置完成后建议用 https://www.ssllabs.com/ssltest/ 检测评分。
三、HTTP安全响应头
add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header X-XSS-Protection "1; mode=block" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:;" always; add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=()" always; add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
各头部作用说明:
- X-Frame-Options: 防止点击劫持攻击
- X-Content-Type-Options: 防止MIME类型混淆攻击
- Content-Security-Policy: 防止XSS和数据注入攻击(核心防护)
- Strict-Transport-Security: 强制HTTPS访问,防止SSL剥离攻击
- Permissions-Policy: 限制浏览器API权限,防止隐私泄露
四、请求大小与超时限制
# 限制请求体大小(防止大文件DoS攻击) client_max_body_size 10m; # 超时设置(防止慢速攻击Slow HTTP DoS) client_body_timeout 10s; client_header_timeout 10s; send_timeout 10s; keepalive_timeout 65s; # 限制连接数(同一IP并发连接) limit_conn_zone $binary_remote_addr zone=addr:10m; limit_conn addr 10; # 限制请求速率 limit_req_zone $binary_remote_addr zone=one:10m rate=30r/s; limit_req zone=one burst=20 nodelay;
慢速HTTP攻击(Slowloris)通过保持连接不关闭耗尽服务器资源,上述超时配置能有效防御。
五、目录与文件权限控制
# 禁止访问隐藏文件
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# 禁止访问敏感文件
location ~* \.(?:bak|config|sql|dump|log|old|swp|git|svn)$ {
deny all;
}
# 限制特定目录的可执行权限
location ~* /(?:uploads|files|assets)/.*\.(?:php|pl|py|jsp|asp)$ {
deny all;
}
# 限制wp-admin等敏感路径(如果有)
location /admin {
allow 你的办公IP;
deny all;
}
六、反向代理安全
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 防止HTTP头注入
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# 禁用代理缓冲(适用于实时应用)
proxy_buffering off;
}
# 限制后端访问来源
location /backend/ {
allow 127.0.0.1;
allow 内网IP段/24;
deny all;
proxy_pass http://internal_backend;
}
七、ModSecurity WAF集成(可选)
# 安装ModSecurity apt install libmodsecurity3 nginx-mod-stream-modsecurity # 加载模块 load_module modules/ngx_stream_modsecurity_module.so; # 在http块启用 modsecurity on; modsecurity_rules_file /etc/nginx/modsec/main.conf; # 核心规则集(OWASP CRS) # 建议启用OWASP Core Rule Set,可防御SQL注入、XSS、命令注入等 SecRuleEngine On Include /etc/nginx/modsec/crs-setup.conf Include /etc/nginx/modsec/rules/*.conf
八、日志安全监控
# 自定义日志格式包含安全相关字段
log_format security '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time $upstream_response_time';
# 安全事件专用日志
access_log /var/log/nginx/access.log security;
error_log /var/log/nginx/error.log warn;
# 使用auditbeat或goaccess做日志分析
# goaccess /var/log/nginx/access.log -o /var/www/html/report.html --real-time-html
九、定期安全巡检命令
# 检查配置语法 nginx -t # 检查监听端口 ss -tlnp | grep nginx # 检查TLS证书到期 echo | openssl s_client -servername yourdomain.com -connect yourdomain.com:443 2>/dev/null | openssl x509 -noout -dates # 检查是否有可疑请求 tail -f /var/log/nginx/access.log | grep -E "(union.*select|eval|base64|