首页主机资讯Ubuntu Nginx如何实现安全防护

Ubuntu Nginx如何实现安全防护

时间2025-12-09 12:44:04发布访客分类主机资讯浏览587
导读:Ubuntu 上 Nginx 的安全防护实践清单 一 系统与网络防护 更新系统与内核,减少已知漏洞面:执行 sudo apt update && sudo apt upgrade -y。 启用 UFW 防火墙,仅开放必要...

Ubuntu 上 Nginx 的安全防护实践清单


一 系统与网络防护

  • 更新系统与内核,减少已知漏洞面:执行 sudo apt update & & sudo apt upgrade -y。
  • 启用 UFW 防火墙,仅开放必要端口:
    • 推荐:sudo ufw allow ‘Nginx Full’(等价于放行 80/443
    • 或分别放行:sudo ufw allow 80/tcp;sudo ufw allow 443/tcp
    • 放行 SSH:sudo ufw allow 22/tcp(务必先确保 SSH 可用再启用)
    • 启用:sudo ufw enable;查看状态:sudo ufw status
  • 如需限制管理面,仅允许特定 IP 访问管理端口(示例):sudo ufw allow from to any port 22,443。
  • 建议开启自动安全更新:sudo apt install -y unattended-upgrades & & sudo dpkg-reconfigure --priority=low unattended-upgrades。
    以上步骤可显著降低暴露面并提升入侵门槛。

二 Nginx 基础加固

  • 隐藏版本信息:在 http 块中添加 server_tokens off; ,减少攻击者信息搜集价值。
  • 限制可用 HTTP 方法:在 server 块中添加
    if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 444; }
    仅保留业务必需方法,阻断非常规方法滥用。
  • 强化请求与连接超时:
    client_body_timeout 12; client_header_timeout 12; keepalive_timeout 15; send_timeout 10;
  • 限制请求速率防滥用:
    http { limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s; }
    server { location / { limit_req zone=one burst=5 nodelay; } }
  • 安全响应头(降低点击劫持、MIME 嗅探等风险):
    add_header X-Frame-Options “SAMEORIGIN”;
    add_header X-Content-Type-Options “nosniff”;
  • 禁止访问敏感与隐藏文件:
    location ~ /. { deny all; }
    这些基础项成本低、收益高,应作为默认配置。

三 加密传输与 HTTPS 部署

  • 使用 Let’s Encrypt 获取并自动配置证书:
    sudo apt install -y certbot python3-certbot-nginx
    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
    证书自动续期建议定期执行 dry-run 验证:sudo certbot renew --dry-run。
  • 推荐的 SSL/TLS 配置片段(在 443 端口 server 块中):
    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    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_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    上述配置兼顾兼容性与安全性,建议全站启用 HTTPS 并开启 HTTP/2

四 请求过滤与 WAF 能力

  • 网关层基础特征拦截(示例,按需扩展):
    • 定义匹配变量:set $block_sql_injection 0; set $block_xss 0;
    • SQL 注入特征:
      if ($request_uri ~* “(union|select|insert|update|delete|drop|truncate|or|and|exec|xp_cmdshell)”) { set $block_sql_injection 1; }
      if ($args ~* “(union|select|insert|update|delete|drop|truncate|or|and|exec|xp_cmdshell)”) { set $block_sql_injection 1; }
    • XSS 特征:
      if ($request_uri ~* “||< img src=|onclick=|onload=|javascript:”) { set $block_xss 1; }
      if ($args ~* “||< img src=|onclick=|onload=|javascript:”) { set $block_xss 1; }
    • 触发拦截时返回 403:if ($block_sql_injection = 1) { return 403; } 等。
  • 进阶方案:部署 ModSecurity-nginx 连接器 + OWASP CRS,为站点提供规则化的 WAF 防护(拦截 SQLi、XSS、RFI 等)。部署要点:
    • 安装 ModSecurity 与连接器,重新编译 Nginx 加载模块;
    • 启用规则引擎并加载 CRS:SecRuleEngine On,配置审计日志;
    • 在 server 块中开启:modsecurity on; 并重启 Nginx 验证。
      请求过滤与 WAF 可显著减少常见 Web 攻击面,建议与业务特性联合调优以避免误报。

五 日志监控与运维安全

  • 结构化日志与告警:
    • 定义日志格式并在 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;
    • 结合 fail2ban 对异常状态码/频繁访问进行封禁(如针对 4xx/5xx 或高频 URI)。
  • 配置与备份:
    • 变更前语法检查:sudo nginx -t;变更后热加载:sudo systemctl reload nginx;
    • 定期备份配置与证书:
      tar -czvf nginx-backup-$(date +%F).tar.gz /etc/nginx /var/log/nginx
    • 设置定时备份任务(示例每日 2 点):
      echo “0 2 * * * tar -czvf /backup/nginx-$(date +%F).tar.gz /etc/nginx /var/log/nginx” | sudo tee -a /etc/crontab
      完善的日志、监控与备份策略有助于快速发现与恢复安全事件。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Ubuntu Nginx如何实现安全防护
本文地址: https://pptw.com/jishu/767002.html
CPUInfo对系统性能有何影响 怎样在Ubuntu上优化Nginx配置

游客 回复需填写必要信息