Nginx 安全配置与加固完全指南
Nginx 是最流行的 Web 服务器之一。错误的 Nginx 配置可能导致目录遍历、SSRF、HTTP 请求走私、信息泄露等安全问题。
一、常见安全风险
| 风险类型 | 描述 | 严重程度 |
|---|---|---|
| 目录遍历 | alias 配置错误 | 高 |
| SSRF 辅助 | 代理配置不当 | 高 |
| HTTP 请求走私 | proxy_pass 与客户端不一致 | 高 |
| 版本泄露 | server_tokens 未关闭 | 低 |
| 信息泄露 | location 配置不当 | 中 |
| 解析漏洞 | 与 PHP-FPM 配合不当 | 高 |
二、基础安全配置
2.1 隐藏版本信息
NGINX
http {
server_tokens off;
# 自定义错误页面
error_page 400 403 404 /error/404.html;
error_page 500 502 503 504 /error/50x.html;
}
2.2 限制请求大小和超时
NGINX
http {
client_max_body_size 10M;
client_body_timeout 10s;
client_header_timeout 10s;
keepalive_timeout 65;
send_timeout 10s;
large_client_header_buffers 4 8k;
}
2.3 禁用不安全方法
NGINX
server {
if ($request_method !~ ^(GET|POST|HEAD)$) {
return 405;
}
location /api/ {
limit_except GET POST {
deny all;
}
}
}
2.4 安全 Headers
NGINX
server {
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options SAMEORIGIN;
add_header X-XSS-Protection "1; mode=block";
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self';" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
}
三、路径安全
3.1 防止目录遍历
NGINX
# 错误配置:alias + 路径结尾不匹配
location /static/ {
alias /home/user/files;
# 请求 /static/../etc/passwd -> /home/user/files/../etc/passwd
}
# 正确配置
location /static/ {
alias /home/user/files/;
}
# 推荐:使用 root 而非 alias
location /static/ {
root /var/www;
}
3.2 禁止访问敏感文件
NGINX
# 禁止访问 .git .svn 等
location ~ /\.(git|svn|hg|idea) {
deny all;
return 404;
}
# 禁止 .env 文件
location ~ \.env$ {
deny all;
return 404;
}
# 禁止备份文件
location ~ \.(bak|old|swp|sql|log)$ {
deny all;
return 404;
}
# 禁止隐藏文件
location ~ /\. {
deny all;
return 404;
}
# 限制配置文件内部访问
location ~ /(config|database|backup)/ {
internal;
}
四、代理安全
4.1 防止 SSRF
NGINX
# 推荐:硬编码后端地址
location /api/backend/ {
proxy_pass http://backend_server:8080/;
proxy_set_header Host backend.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 危险:从用户参数获取目标
location /api/proxy/ {
proxy_pass http://$arg_target; # SSRF 风险!
}
4.2 HTTP 请求走私防护
NGINX
location /api/ {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://backend:8080;
}
五、SSL/TLS 配置
NGINX
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
# 仅安全版本
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;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/dhparam.pem;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
# 会话缓存
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
}
# HTTP -> HTTPS 强制跳转
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
六、限流与防攻击
6.1 请求限流
NGINX
http {
# 定义限流区域
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/s;
limit_req_zone $binary_remote_addr zone=api:10m rate=30r/s;
limit_conn_zone $binary_remote_addr zone=conn:10m;
server {
location /api/login {
limit_req zone=login burst=10 nodelay;
limit_req_status 429;
}
location /api/ {
limit_req zone=api burst=50 nodelay;
limit_conn conn 10;
}
# 下载限速
location /downloads/ {
limit_rate 500k;
limit_rate_after 1m;
}
}
}
6.2 IP 黑白名单
NGINX
location /admin/ {
allow 192.168.1.0/24;
allow 10.0.0.0/8;
deny all;
}
# 使用 geo 模块
geo $block_ip {
default 0;
10.0.0.0/8 1;
172.16.0.0/12 1;
192.168.0.0/16 1;
}
server {
if ($block_ip) { return 403; }
}
七、日志配置
NGINX
http {
log_format security '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time';
access_log /var/log/nginx/access.log security;
# 只记录错误状态码
map $status $loggable {
400 1; 401 1; 403 1; 404 1;
500 1; 502 1; 503 1;
default 0;
}
access_log /var/log/nginx/error_access.log security if=$loggable;
}
八、总结
| 配置项 | 风险 | 推荐配置 |
|---|---|---|
| server_tokens | 版本泄露 | off |
| autoindex | 目录列表 | off |
| alias/root | 目录遍历 | 使用 root + 检查路径 |
| proxy_pass | SSRF | 硬编码后端地址 |
| SSL 协议 | 弱加密 | TLSv1.2+ |
| 请求限制 | DDoS | 启用限流 |