在Ubuntu上部署Nginx时,需通过基础配置加固、SSL/TLS加密、访问控制、防火墙联动、日志监控及高级防护等多层措施,全面提升服务器安全性。以下是具体实现步骤:
隐藏Nginx版本信息
编辑Nginx主配置文件(/etc/nginx/nginx.conf),在http块中添加server_tokens off;,禁用响应头中的版本标识(如Server: nginx/1.18.0 (Ubuntu))。此操作可防止攻击者通过版本号识别已知漏洞,降低针对性攻击风险。
限制HTTP方法
在server或location块中,通过if指令过滤非法HTTP方法(如PUT、DELETE),仅允许必要的GET、HEAD、POST方法:
if ($request_method !~ ^(GET|HEAD|POST)$) { return 444; # 直接关闭连接 } 此配置可阻断通过非法方法发起的攻击(如文件上传漏洞利用)。
配置合理超时设置
在nginx.conf的http块中,调整以下超时参数,防止慢速攻击(如Slowloris)耗尽服务器资源:
client_body_timeout 12; # 请求体读取超时(秒) client_header_timeout 12; # 请求头读取超时(秒) keepalive_timeout 15; # 长连接保持时间(秒) send_timeout 10; # 响应发送超时(秒) 这些参数需根据业务场景调整,平衡安全性与用户体验。
限制请求体与并发连接
server或location块中添加client_max_body_size 10M;,防止大文件上传(如超过10MB的恶意文件)耗尽磁盘空间。limit_conn_zone和limit_conn指令,限制单个IP的并发连接数(如每个IP最多1个连接):http { limit_conn_zone $binary_remote_addr zone=ops:10m; # 定义共享内存区域 } server { location / { limit_conn ops 1; # 限制并发连接数 } } 此配置可防止DDoS攻击或恶意爬虫占用过多服务器资源。启用HTTPS并强制跳转
yourdomain.com为实际域名):sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com Certbot会自动修改Nginx配置,启用443端口的HTTPS监听。/etc/nginx/sites-available/default),添加以下server块:server { listen 80; server_name yourdomain.com www.yourdomain.com; return 301 https://$host$request_uri; # 永久重定向至HTTPS } 此配置确保所有流量均通过加密通道传输。优化SSL/TLS参数
在HTTPS的server块中,配置安全的加密协议与算法:
ssl_protocols TLSv1.2 TLSv1.3; # 禁用不安全的TLS 1.0/1.1 ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384'; # 使用强加密套件 ssl_prefer_server_ciphers on; # 优先使用服务器端加密套件 ssl_session_timeout 1d; # 会话超时时间(1天) ssl_session_cache shared:SSL:50m; # 会话缓存大小(50MB) ssl_stapling on; # 启用OCSP装订(提升证书验证效率) 这些配置可防止降级攻击(如POODLE),提升加密安全性。
配置HSTS
添加Strict-Transport-Security头部,强制浏览器使用HTTPS(即使用户手动输入HTTP):
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; max-age=31536000表示有效期为1年,includeSubDomains覆盖所有子域名,有效预防SSL剥离攻击。
基于IP的白名单/黑名单
/admin/),仅允许内网IP访问:location /admin/ { allow 192.168.1.0/24; # 允许内网IP段 allow 10.0.0.0/8; # 允许另一个内网IP段 deny all; # 拒绝其他所有IP } deny指令直接拒绝特定IP(如攻击源IP):location / { deny 192.168.1.100; # 屏蔽单个IP allow all; } 此配置可防止未经授权的访问。限制请求频率
使用limit_req_zone和limit_req指令,限制单个IP的请求频率(如每秒最多1个请求,允许突发5个):
http { limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s; # 定义共享内存区域与速率 } server { location / { limit_req zone=mylimit burst=5 nodelay; # 限制请求频率 proxy_pass http://backend; } } 此配置可有效防御暴力破解(如密码猜测)和DDoS攻击。
安装与配置UFW
UFW(Uncomplicated Firewall)是Ubuntu的简易防火墙工具,可通过以下命令配置:
sudo apt install ufw sudo ufw allow 22/tcp # 允许SSH(远程管理) sudo ufw allow 80/tcp # 允许HTTP(可选,若未启用HTTPS) sudo ufw allow 443/tcp # 允许HTTPS sudo ufw enable # 启用防火墙 sudo ufw status # 查看状态(确认规则正确) 此配置可阻止未经授权的网络流量访问服务器。
配置iptables高级规则
若需更细粒度的控制,可使用iptables限制连接速率(如每个IP每秒最多5个新连接):
sudo iptables -A INPUT -p tcp --dport 443 -m connlimit --connlimit-above 5 -j DROP sudo iptables -A INPUT -p tcp --dport 443 -m limit --limit 5/min -j ACCEPT 此配置可防止大量并发连接耗尽服务器资源。
增强型日志配置
自定义Nginx日志格式,包含客户端IP、请求时间、响应状态、请求体大小等信息,便于后续分析:
log_format security '$remote_addr - $http_x_forwarded_for - $time_iso8601 ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" $request_time'; access_log /var/log/nginx/security.log security; # 记录安全相关日志 error_log /var/log/nginx/error.log crit; # 记录错误日志(仅严重级别) 此配置可帮助快速定位异常请求(如大量404错误)。
集成Fail2Ban
Fail2Ban是一款入侵检测工具,可自动屏蔽恶意IP(如频繁访问404页面的IP)。
sudo apt install fail2ban/etc/fail2ban/filter.d/nginx-badbots.conf):[Definition] failregex = ^<HOST> -.*"(GET|POST).*HTTP.*" 404 .*$ ignoreregex = /etc/fail2ban/jail.local):[nginx-badbots] enabled = true filter = nginx-badbots logpath = /var/log/nginx/access.log maxretry = 2 # 2次错误后屏蔽 bantime = 86400 # 屏蔽1天 sudo systemctl restart fail2bansudo apt install libmodsecurity3 libmodsecurity-dev sudo git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git cd ModSecurity-nginx sudo make && sudo make install /etc/nginx/nginx.conf的http块中加载模块:load_module modules/ngx_http_modsecurity_module.so;server块中启用ModSecurity并指定规则文件:server { modsecurity on; modsecurity_rules_file /etc/nginx/modsec/main.conf; } /etc/nginx/modsec/main.conf):sudo mkdir /etc/nginx/modsec cd /etc/nginx/modsec sudo git clone --depth 1 https://github.com/coreruleset/coreruleset.git sudo cp coreruleset/crs-setup.conf.example coreruleset/crs-setup.conf sudo cp coreruleset/rules/*.conf /etc/nginx/modsec/coreruleset/ echo -e "SecRuleEngine On\nSecRequestBodyAccess On\nSecResponseBodyAccess On\nInclude /etc/nginx/modsec/coreruleset/crs-setup.conf\nInclude /etc/nginx/modsec/coreruleset/rules/*.conf" > /etc/nginx/modsec/main.conf sudo systemctl restart nginx通过以上步骤,可全面加固Ubuntu上Nginx的安全性,降低被攻击的风险。需定期更新Nginx、SSL证书及系统组件,确保安全配置的有效性。