HTTP 是明文传输,存在被窃听和篡改的风险。HTTPS 通过 TLS/SSL 协议对通信加密,是现代网站的标准配置。浏览器对 HTTP 站点会标记"不安全"。

一、TLS 握手简述

  1. 客户端发起连接,告知支持的加密算法
  2. 服务器返回证书(含公钥)
  3. 客户端验证证书合法性(CA 签名)
  4. 双方协商出对称密钥,后续通信用它加密

二、用 Certbot 申请 Let's Encrypt 免费证书

dnf install -y certbot python3-certbot-nginx
certbot --nginx -d example.com -d www.example.com

Certbot 会自动修改 Nginx 配置、配置证书路径和 https 跳转,并设置自动续期。

三、手动续期测试

certbot renew --dry-run    # 模拟续期,验证定时任务正常
Let's Encrypt 证书有效期 90 天,Certbot 安装时会自动配置 systemd timer 定时续期,无需手动操心。

四、Nginx 关键配置片段

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}

五、小结

HTTPS 已经平民化,Let's Encrypt 让任何网站都能免费、自动地用上加密。强烈建议所有站点尽快上 HTTPS。