SSL/TLS 证书部署与 HTTPS 配置指南

一、核心概念
  1. SSL/TLS 协议
    通过非对称加密建立安全通道,保护数据传输过程。客户端验证服务器身份时需检查证书有效性,满足: $$ \text{验证通过} \iff \begin{cases} \text{证书由可信CA签发} \ \text{域名匹配} \ \text{证书未过期} \end{cases} $$

  2. Let's Encrypt 优势

    • 免费自动化证书颁发
    • 支持 ACME 协议自动续期
    • 90 天有效期(需配置自动更新)

二、证书部署流程(以 Nginx 为例)
步骤 1:安装 Certbot 工具
# Ubuntu/Debian
sudo apt install certbot python3-certbot-nginx

# CentOS
sudo yum install certbot python3-certbot-nginx

步骤 2:获取证书
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

  • 交互式向导将自动修改 Nginx 配置
  • 证书存储在 /etc/letsencrypt/live/yourdomain.com/
步骤 3:验证 Nginx 配置
server {
    listen 443 ssl;
    server_name yourdomain.com;
    
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    
    # 强制启用 TLS 1.2+
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
}

步骤 4:HTTP 重定向到 HTTPS
server {
    listen 80;
    server_name yourdomain.com;
    return 301 https://$host$request_uri;
}


三、证书自动续期
  1. 测试续期命令
    sudo certbot renew --dry-run
    

  2. 添加定时任务(每月执行)
    # 编辑 crontab
    sudo crontab -e
    # 添加行
    0 0 1 * * /usr/bin/certbot renew --quiet
    


四、安全加固建议
  1. HSTS 头部
    在 Nginx 配置中添加:
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    

  2. 密钥轮换
    每 3 个月手动更新 ECDH 参数:
    openssl ecparam -genkey -name secp384r1 | tee /etc/ssl/private/dhparam.pem
    

    在 Nginx 配置中引用:
    ssl_dhparam /etc/ssl/private/dhparam.pem;
    


五、验证配置
  1. 在线检测工具
    SSL Labs Server Test
  2. 本地命令验证
    curl -I https://yourdomain.com
    # 应返回 HTTP/2 200
    

关键提示:完成部署后需清除浏览器缓存,首次访问可能出现证书信任提示,刷新后消失。若遇证书错误,检查服务器时间是否同步(使用 ntpdate 校准)。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐