问题:默认配置可能暴露服务器信息
# 错误示例
server_tokens on;
# 正确配置
server_tokens off;
proxy_hide_header X-Powered-By;
more_clear_headers Server;
问题:允许不必要的方法(如TRACE)可能导致安全风险
# 正确配置
location / {
limit_except GET POST {
deny all;
}
}
问题:未禁用目录列表可能泄露敏感文件
# 正确配置
autoindex off;
问题:使用弱加密算法或旧协议
# 推荐配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256...';
ssl_prefer_server_ciphers on;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
问题:Nginx进程以root运行或配置文件权限过宽
# 正确做法
chown -R nginx:nginx /etc/nginx
chmod -R 640 /etc/nginx
find /etc/nginx -type d -exec chmod 750 {} \;
# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN";
# XSS防护
add_header X-XSS-Protection "1; mode=block";
# 内容安全策略
add_header Content-Security-Policy "default-src 'self';";
# MIME类型嗅探防护
add_header X-Content-Type-Options nosniff;
# 防止暴力破解
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
server {
location /login {
limit_req zone=one burst=20;
}
}
# IP白名单
location /admin {
allow 192.168.1.0/24;
allow 10.0.0.1;
deny all;
}
nginx
access_log /var/log/nginx/access.log combined buffer=32k flush=5m;
error_log /var/log/nginx/error.log warn;
bash
nginx -t
nginx
worker_processes auto;
worker_rlimit_nofile 100000;
events {
worker_connections 4096;
multi_accept on;
}
快速禁用站点:
mv /etc/nginx/conf.d/vulnerable_site.conf /etc/nginx/conf.d/vulnerable_site.conf.disabled
nginx -s reload
紧急限制访问:
location / {
return 503;
}
通过遵循这些指南,您可以显著提高Nginx服务器的安全性,避免常见的安全陷阱。