Skip to content

HTTPS 证书 ​

现状 ​

  • 证书颁发机构:Let's Encrypt
  • 证书名(lineage):emphant.com
  • 覆盖域名:emphant.com、www.emphant.com、m.emphant.com、docs.emphant.com、blog.emphant.com
  • 密钥类型:ECDSA
  • 文件位置:/etc/letsencrypt/live/emphant.com/fullchain.pem
bash
certbot certificates

为什么用一张证书 ​

所有站点在同一台机器、同一个 IP,用一张多域名证书:

  • nginx 只需要配一组 ssl_certificate / ssl_certificate_key;
  • 续期一次全部生效,不会出现某个子域证书过期没人发现;
  • SNI 只有一个证书需要加载。

新增子域时扩展证书 ​

bash
certbot certonly --nginx \
  --cert-name emphant.com \
  -d emphant.com -d www.emphant.com -d m.emphant.com \
  -d docs.emphant.com -d blog.emphant.com

--cert-name 指定已有的 lineage,certbot 会把新域名并入同一张证书(可能提示是否扩展,选扩展)。certonly 表示只申请证书,不改 nginx 配置(配置由我们自己的模板管理)。

执行前确认:

  • 子域已解析到本机;
  • 80 端口可访问(HTTP-01 验证);
  • nginx 配置里存在对应 server_name 的 80 端口 server,certbot 才能自动加验证 location。

自动续期 ​

certbot 安装时通常已写入 systemd timer:

bash
systemctl list-timers | grep certbot
systemctl status certbot.timer

手动演练(不会真的续期,只验证流程):

bash
certbot renew --dry-run

续期成功后需要 reload nginx 才会加载新证书:

bash
# /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
#!/usr/bin/env bash
nginx -t && systemctl reload nginx
bash
chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

验证 ​

bash
echo | openssl s_client -connect docs.emphant.com:443 -servername docs.emphant.com 2>/dev/null \
  | openssl x509 -noout -subject -dates

或直接看线上:

bash
curl -sI https://docs.emphant.com/ | head -5

常见问题 ​

现象原因处理
NET::ERR_CERT_COMMON_NAME_INVALID证书没覆盖该子域扩展证书后 reload
续期失败:Invalid response ... 40480 端口 vhost 被默认 server 吞掉确认子域 server 块存在且 server_name 正确
续期成功但浏览器仍是旧证书没 reload nginx配置 deploy hook
too many certificates already issued短时间内反复申请等一周或使用 staging 环境调试
HTTP 仍可访问80 server 没写 301补 return 301 https://$host$request_uri;

安全参数 ​

nginx
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:...;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;

没有开 HSTS:站点还在迭代期,避免证书出问题时把用户锁在 HTTPS 之外。等配置稳定后再考虑加 Strict-Transport-Security。

文档随仓库一起更新,站点内容全部在浏览器本地运行。