温馨提示×

nginx ssl在ubuntu上的最佳实践

小樊
46
2025-11-23 07:44:01
栏目: 云计算

Ubuntu 上 Nginx SSL 最佳实践

一 准备与证书获取

  • 安装 Nginx 与 Certbot
    • 执行:sudo apt update && sudo apt install nginx certbot python3-certbot-nginx
    • 启动并设为开机自启:sudo systemctl start nginx && sudo systemctl enable nginx
  • 获取并自动配置证书
    • 单域名:sudo certbot --nginx -d yourdomain.com
    • 含 www:sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
    • 自动续期测试:sudo certbot renew --dry-run
  • 防火墙放行
    • UFW:sudo ufw allow 'Nginx Full'
    • firewalld:sudo firewall-cmd --permanent --add-service=https && sudo firewall-cmd --reload
  • 说明
    • Let’s Encrypt 证书路径通常为:/etc/letsencrypt/live/yourdomain.com/fullchain.pem(证书链)与 /etc/letsencrypt/live/yourdomain.com/privkey.pem(私钥)。

二 推荐的 Nginx SSL 配置

  • 使用配置片段管理证书与参数(便于复用与维护)
    • 证书片段(/etc/nginx/snippets/ssl-yourdomain.conf)
      • ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
      • ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    • 参数片段(/etc/nginx/snippets/ssl-params.conf,见下节“关键参数说明”)
  • 站点示例(/etc/nginx/sites-available/yourdomain 或 /etc/nginx/conf.d/yourdomain.conf)
    • 强制 HTTP→HTTPS
      server {
        listen 80;
        server_name yourdomain.com www.yourdomain.com;
        return 301 https://$host$request_uri;
      }
      
    • HTTPS 主配置
      server {
        listen 443 ssl http2;
        server_name yourdomain.com www.yourdomain.com;
      
        include snippets/ssl-yourdomain.conf;
        include snippets/ssl-params.conf;
      
        # 可选:OCSP 装订信任链(Let’s Encrypt 中间证书)
        # ssl_trusted_certificate /etc/letsencrypt/live/yourdomain.com/chain.pem;
      
        root /var/www/yourdomain.com;
        index index.html;
      
        location / {
          try_files $uri $uri/ =404;
        }
      }
      
    • 检查与生效
      • 语法检查:sudo nginx -t
      • 重载配置:sudo systemctl reload nginx
  • 说明
    • 启用 HTTP/2 可提升 HTTPS 性能(需 Nginx ≥ 1.9.5 且 OpenSSL ≥ 1.0.2)。

三 关键参数说明与推荐值

  • 协议与套件
    • 仅启用安全协议:ssl_protocols TLSv1.2 TLSv1.3;
    • 优先服务器套件:ssl_prefer_server_ciphers on;
    • 现代套件(TLS 1.3 优先,1.2 兼容):
      • ssl_ciphersuites TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256;(TLS 1.3)
      • ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;(TLS 1.2)
  • 密钥交换与参数
    • 椭圆曲线:ssl_ecdh_curve secp384r1;
    • DH 参数(可选但推荐):生成 sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 4096,并在 server 块加入 ssl_dhparam /etc/ssl/certs/dhparam.pem;
  • 会话与握手优化
    • 会话缓存:ssl_session_cache shared:SSL:10m;
    • 会话超时:ssl_session_timeout 10m;
    • 会话票据:ssl_session_tickets off;(多数场景更利于安全与一致性)
  • 在线证书状态校验
    • OCSP Stapling:ssl_stapling on; ssl_stapling_verify on; resolver 8.8.8.8 8.8.4.4 valid=300s; resolver_timeout 5s;
  • 安全响应头
    • HSTS:add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;(确认全站可 HTTPS 后再启用 preload)
    • 其他:add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff"; add_header X-XSS-Protection "1; mode=block";
  • 说明
    • 禁用不安全协议(SSLv2/3、TLS1.0/1.1)与弱套件;TLS 1.3 使用独立的 ssl_ciphersuites 指令。

四 验证与运维

  • 配置与连通性
    • 语法:sudo nginx -t
    • 重载:sudo systemctl reload nginx
    • 访问与抓包:确认 https://yourdomain.com 正常、证书链完整、TLS 版本与套件符合预期
  • 自动化与监控
    • 续期
      • 测试:sudo certbot renew --dry-run
      • 定时:sudo crontab -e 加入 0 12 * * * /usr/bin/certbot renew --quiet
    • 安全与性能测试
      • SSL Labs 评分(目标 A+):https://www.ssllabs.com/ssltest/
      • 持续监控证书到期与站点可用性(告警、日志审计)
  • 常见问题排查
    • 证书路径错误、权限不足、域名解析或端口 443 不通、Nginx 语法错误、续期日志异常(/var/log/letsencrypt/)。

0