Ubuntu Nginx安全加固实战指南
/etc/nginx/nginx.conf,在http块中添加server_tokens off;,避免攻击者通过HTTP响应头获取版本号,减少针对性漏洞攻击风险。修改后需执行sudo nginx -s reload使配置生效。server或location块中添加if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 444; },拒绝PUT、DELETE等不常用且危险的HTTP方法,降低非法操作的可能性。add_header X-Frame-Options "SAMEORIGIN";:防止网页被嵌入iframe(点击劫持攻击);add_header X-XSS-Protection "1; mode=block";:启用浏览器XSS防护;add_header X-Content-Type-Options "nosniff";:禁止浏览器嗅探内容类型(避免MIME类型混淆攻击);add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";:强制浏览器仅通过HTTPS访问(HSTS,防范SSL剥离攻击)。http块中优化超时参数,减少资源占用和慢速攻击风险:client_body_timeout 12;(请求体读取超时)、client_header_timeout 12;(请求头读取超时)、keepalive_timeout 15;(长连接保持时间)、send_timeout 10;(响应发送超时)。allow/deny指令限制敏感路径的访问,例如管理后台仅允许可信IP访问:location /admin/ {
allow 192.168.1.0/24; # 内网IP段
allow 10.0.0.0/8; # 其他可信IP段
deny all; # 拒绝其余IP
}
limit_conn_zone和limit_conn指令控制单个IP的并发连接数,防止资源耗尽攻击:http {
limit_conn_zone $binary_remote_addr zone=conn_limit:10m; # 定义共享内存区域
server {
location / {
limit_conn conn_limit 10; # 每个IP最多10个并发连接
}
}
}
limit_req_zone和limit_req指令限制请求速率,防范暴力破解(如密码猜测)和DDoS攻击:http {
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s; # 每秒10个请求
server {
location /login/ {
limit_req zone=req_limit burst=20 nodelay; # 允许突发20个请求,直接拒绝超限
}
}
}
map模块实现动态黑白名单,例如将恶意IP加入黑名单并返回403:http {
map $remote_addr $blacklist {
default 0;
123.456.789.11 1; # 黑名单IP
123.456.789.21 1;
}
server {
location / {
if ($blacklist) { return 403; } # 匹配黑名单则拒绝访问
}
}
}
sudo apt install certbot python3-certbot-nginx),获取证书后修改配置:server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri; # HTTP跳转HTTPS
}
server块中配置强加密套件和协议,提升数据传输安全性:ssl_protocols TLSv1.2 TLSv1.3; # 仅使用TLS 1.2及以上版本
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384'; # 强加密套件
ssl_prefer_server_ciphers on; # 优先使用服务器端加密套件
ssl_session_cache shared:SSL:10m; # 会话缓存
ssl_session_timeout 10m; # 会话超时时间
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; # 证书路径
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # 私钥路径
http2,提升多路复用效率:listen 443 ssl http2;sudo apt update && sudo apt upgrade nginx,确保Nginx及依赖库(如OpenSSL)为最新版本,修补已知安全漏洞。sudo apt install ufw
sudo ufw allow 22/tcp # SSH
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
sudo ufw enable # 启用防火墙
--without-http_*选项移除不需要的模块(如autoindex、gzip_static),减少攻击面。若使用预编译包,可通过nginx -V查看已加载模块,避免加载无用模块。http块中定义日志格式和路径,记录访问和错误信息:http {
log_format main '$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/access.log main; # 访问日志
error_log /var/log/nginx/error.log warn; # 错误日志(级别设为warn及以上)
}
grep、awk等工具分析日志,重点关注404(未找到)、403(禁止访问)、500(服务器错误)等异常状态码,及时发现恶意扫描或攻击行为:tail -f /var/log/nginx/access.log | grep -E '404|500|403'sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
tar命令备份Nginx配置和日志文件,存储到安全位置(如外部存储设备或云存储):sudo tar -czvf nginx-backup-$(date +%F).tar.gz /etc/nginx /var/log/nginx
设置cron任务实现每日自动备份:echo "0 2 * * * tar -czvf /backup/nginx-$(date +\%F).tar.gz /etc/nginx /var/log/nginx" | sudo tee -a /etc/crontab