给内网服务用上 HTTPS,我写了个泛域名证书分发工具 CertPost

上一篇写到用 iPhone 连 VPN 回家。连上以后,假设我要打开 https://nas.example.com 看照片,手机已经能找到 NAS,浏览器还得检查它拿出来的证书。等家里再添一个监控面板、一台跑 Nginx 的小主机,我还得再查一件事。证书续好了,哪几台机器还在用旧的那份?我写了个小工具叫证驿 CertPost,把签证书和送证书这两件事一起做了。上一篇:用证书连回家 · CertPost 项目地址
为什么选泛域名证书?
拿上面的 NAS 举例。它只在家里和 VPN 里能访问。如果用 HTTP-01 验证,Let’s Encrypt 得从公网访问 NAS 的 80 端口,读到指定 URL 下的验证文件。我想让这个端口继续关着,于是选了 DNS-01。签发时在公网权威 DNS 上临时加一条 _acme-challenge.example.com 的 TXT 记录,证书机构查到正确的值就能完成验证。NAS 的地址照样可以只写在家里的 DNS 里。Let’s Encrypt 验证方式
既然用了 DNS 验证,顺手就能申请 *.example.com。今天加 photos.example.com,明天把监控面板搬到 status.example.com,手头这张证书都能用。几十个服务一起重装时,每台机器各自申请可能会碰到免费证书的签发限额。从 CertPost 取已有证书,给新机器装十次也不用再向 CA 下一个订单。这里有个容易忽略的细节,星号只管一层。CertPost 会把 example.com 也写进同一张证书,要给 nas.home.example.com 用,则要另外添加 home.example.com。Let’s Encrypt 签发限额
还有个我很在意的细节。单独给 nas.example.com 签一张公信证书,这个名字就会出现在公开的证书透明度(CT)日志里。有人专门跟着这些日志扫描新出现的主机。申请泛域名证书,日志里能看到 example.com 和 *.example.com,家里给相册起的具体名字就不用跟着公开了。服务是否允许外部访问,仍然由 VPN、防火墙和登录权限控制。CT 日志说明 · 关于 CT 扫描的实测
一张证书怎么送到 Nginx 上?
先在 CertPost 后台加一份 DNS 凭据。目前支持阿里云 DNS 的 RAM AccessKey 和 Cloudflare 的 API Token。再到域名与证书里填域名和联系邮箱,选好刚才的凭据。开启自动签发后,写 TXT、验证、清理记录以及到期前续签,都在服务端完成。设备上只要有自己的客户机令牌,DNS 密钥可以留在服务端。

接下来说这台跑 Nginx 的机器。在后台登记一台客户机,只勾选它要用的证书,按页面提示安装客户端、保存令牌。客户端是 Go 写的,Linux 的 x86_64 和 ARM64 都有单个静态二进制。它跑完就退出,小主机空闲时也省下一份常驻内存。第一次我会手动跑下面两条,看一眼本机实际拿到了什么。把 example.com 换成自己的域名。
/usr/local/bin/cert-client sync
openssl x509 -in /var/lib/cert-client/example.com/current/fullchain.pem -noout -dates
把 Nginx 的证书和私钥路径指到 current/ 下的 fullchain.pem 和 privkey.pem,再给客户端配好同步后脚本,证书有变化时就能先执行 nginx -t,通过后再 reload。启用自带的 systemd 定时器后,设备每小时检查证书,每天发一次心跳。以后要升级客户端程序,重新运行安装脚本就行,原来的配置和令牌会保留。
过一阵想知道哪台机器没跟上,可以先去客户机看最后一次心跳,再去审计记录查它有没有下载新证书。心跳能说明设备联系过服务端,下载记录能说明它取过文件。要确认 Nginx 已经换上新证书,还得看实际 HTTPS 连接返回的有效期。泛域名证书的私钥在获授权的设备间共享,我会尽量只把它发给真正终止 HTTPS 的那几台。
如果你也有一台只在内网开放的服务,可以先拿它试试,从签第一张证书到 Nginx 加载新证书走一遍。服务端的 Docker Compose、客户端安装命令和 Nginx 配置示例都放在仓库里。证驿 CertPost · 代码和部署说明
English
Why I wrote CertPost for HTTPS on a home network
In my last post I set up an iPhone to reach my home network over a VPN. Suppose I connect and open https://nas.example.com to look at my photos. The VPN gets me to the NAS, and the browser still needs a certificate it can trust. Add a monitoring dashboard and a small Nginx box, and I have another question every time the certificate renews. Which machines are still serving the old copy? I wrote CertPost to handle issuance and delivery together. Previous VPN post · CertPost on GitHub
With HTTP-01, Let’s Encrypt needs to fetch a file from port 80. My NAS can keep that port private with DNS-01. The certificate authority checks a temporary TXT record at _acme-challenge.example.com in public DNS. The NAS itself can keep using a private address. DNS-01 also lets me request *.example.com. I can add photos.example.com or move the dashboard to status.example.com using the certificate I already have. Reinstalling ten devices costs no new orders, which helps with issuance limits. The wildcard covers one level. CertPost includes example.com on the certificate as well. For nas.home.example.com, I would add home.example.com separately. Challenge types · Rate limits
There’s also a privacy benefit. Issuing a public certificate for the exact name of my photo service puts that name in Certificate Transparency logs. Scanners do look for new hosts there. A wildcard certificate publishes the base domain and the wildcard, so individual service names can stay in private DNS. The VPN, firewall and authentication still control who can reach them. CT logs · Research on CT-based scanning
Getting the certificate onto the Nginx box
I add an Alibaba Cloud DNS RAM AccessKey or a Cloudflare API token under DNS credentials, then enter a domain and contact email under Domains & certificates. With automatic issuance enabled, the server creates the TXT record, cleans it up after validation and renews before expiry. The DNS credentials stay on the server. Each device gets its own token and a grant for the certificates it needs.
On the Nginx box, I install the static Go client for Linux x86_64 or ARM64 and save the token. The two commands above run the first sync and show the validity dates on disk. Nginx can read fullchain.pem and privkey.pem from the domain’s current/ directory. A sync hook can run nginx -t and reload Nginx when a certificate changes. The systemd timers check certificates hourly and send a daily heartbeat. The client exits between runs. To upgrade the executable, I rerun the installer, which preserves the configuration and token.
When a device looks out of date, I check its last heartbeat under Clients, then its download under Audit log. To confirm what visitors get, I check the dates served over HTTPS. Every device that receives the wildcard certificate gets the same private key, so I grant it only to machines that actually terminate TLS. If you have a private service to try this on, the repository includes the Compose setup, client installer and Nginx example. CertPost code and deployment instructions
如果觉得文章内容比较实用,期望获得更新通知,请关注公众号:
