作为一款高性能的Web服务器和反向代理服务器,Nginx的安全性管理至关重要。以下是确保Nginx健壮性安全管理的全面方案:
版本信息隐藏
server_tokens off;
禁用不必要的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
限制HTTP协议版本
if ($server_protocol ~* "HTTP/1.0") {
return 444;
}
IP访问限制
location /admin {
allow 192.168.1.0/24;
allow 10.0.0.1;
deny all;
}
速率限制
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
location / {
limit_req zone=one burst=20;
}
强密码套件
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384...';
ssl_prefer_server_ciphers on;
HSTS头
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.example.com; img-src 'self' data:;";
XSS防护
add_header X-XSS-Protection "1; mode=block";
MIME类型嗅探防护
add_header X-Content-Type-Options "nosniff";
点击劫持防护
add_header X-Frame-Options "SAMEORIGIN";
禁用目录列表
autoindex off;
敏感文件保护
location ~* \.(htaccess|htpasswd|ini|log|sh|sql)$ {
deny all;
}
详细日志记录
log_format security '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/security.log security;
错误日志监控
error_log /var/log/nginx/error.log warn;
禁用不必要的模块
# 编译时禁用不需要的模块
./configure --without-http_autoindex_module --without-http_ssi_module
限制模块功能
location ~* \.(php|pl|py|jsp|asp|sh|cgi)$ {
deny all;
}
证书自动续期
# 使用certbot设置自动续期
certbot renew --quiet --no-self-upgrade --post-hook "systemctl reload nginx"
配置审计
nginx -t # 测试配置语法
定期更新
apt-get update && apt-get upgrade nginx
Web应用防火墙(WAF)
DDoS防护
limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
limit_conn conn_limit_per_ip 20;
零信任网络访问
通过实施以上措施,可以显著提高Nginx服务器的安全性和健壮性,有效防御各类网络攻击。建议定期进行安全审计和渗透测试,确保配置持续有效。