跳到主要内容

NET::ERR_CERT_DATE_INVALID 完整修复指南:证书过期、NTP 时间漂移与自动续期

问题背景与根因:NET::ERR_CERT_DATE_INVALID 到底是什么

当 Chrome、Edge 打开 HTTPS 站点出现 NET::ERR_CERT_DATE_INVALID,表示浏览器校验证书的 notBefore/notAfter 失败:证书已过期、尚未生效,或客户端与服务器时间偏差过大。公共信任 TLS 证书单张最长有效期目前为 398 天,Let’s Encrypt 免费证书为 90 天,常见故障是未续期、续期后 Web 服务未重载、NTP 失效导致系统时间漂移,或中间证书过期。该错误不是 DNS 错误,也不是混合内容错误,必须先确认浏览器看到的是哪一张证书。

排查顺序建议固定为:线上证书日期 → 服务器 UTC 时间 → 本地证书文件 → 续期任务与 reload 钩子。若使用 CDN、WAF 或负载均衡,源站续期不等于边缘节点已更新,需要在控制台更新证书或触发回源。下面表格列出高频根因与首选检查项。

根因 典型现象 首选检查
站点证书过期 openssl 显示 notAfter 已过 openssl s_client
服务器时间错误 证书 notBefore 在未来 date -u、timedatectl
客户端时间错误 仅个别电脑报错 本机时间与时区
中间证书过期 部分旧客户端报错 完整链 showcerts
续期未重载 文件已新、线上仍旧 nginx -T、reload

快速定位:用 openssl 与 curl 确认证书状态

先不要急着重签。用下面命令分别检查线上证书、服务器时间、本机证书文件,三者对比即可定位。

date -u
timedatectl
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates -subject -issuer -serial
curl -vI https://example.com 2>&1 | grep -E 'expire date|SSL certificate|subject:'
openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -dates
nginx -T | grep -n ssl_certificate

若 notAfter 已过,就是证书过期;若 notBefore 在未来,优先查服务器时间;若本地文件日期正常但线上仍旧,说明 Web 服务未 reload 或 CDN 未更新。检查链时执行 openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null,逐个看中间证书的 notAfter。SNI 站点必须带 -servername,否则可能取到默认站点的旧证书。记录 serial 与 issuer,续期前后对比 serial 可确认是否真的换了证书。若 curl 返回 certificate has expired,直接进入续期流程。

实操一:修复服务器系统时间与 NTP 同步

证书有效期按 UTC 判断,时区不是根因,系统时间漂移才是。先用 date -u 与 timedatectl 看 NTP 是否同步。若 NTPSynchronized=no 或偏移超过 5 分钟,浏览器就可能报日期无效。

sudo timedatectl set-ntp true
sudo systemctl enable --now chronyd
chronyc tracking
chronyc sources -v
sudo chronyc makestep
sudo hwclock --systohc
date -u
timedatectl show -p NTPSynchronized --value

chronyc tracking 中 Leap status : Normal、System time 偏移接近 0 才算正常;chronyc sources -v 的 ^* 表示当前优选源。虚拟机快照恢复、BIOS 电池失效、容器宿主机时间错误都会导致漂移。临时可用 sudo ntpdate -u pool.ntp.org,但生产环境应使用 chrony 持续同步。修改时间后重启 Nginx 并非必须,但浏览器需重新握手;若证书 notBefore 因时间错误被签成未来时间,应重新签发而不是只改时间。

实操二:用 Certbot 续期 Let’s Encrypt 证书

Let’s Encrypt 证书有效期 90 天,Certbot 默认在剩余 30 天内续期,systemd 定时器每天运行两次并带随机延迟。先确认证书清单和到期时间:

sudo certbot certificates
sudo certbot renew --dry-run
sudo systemctl status certbot.timer
sudo systemctl list-timers | grep -i certbot

--dry-run 使用 staging 环境,不消耗正式速率限制。正式续期指定证书名并加部署钩子,确保新证书落地后重载 Web 服务:

sudo certbot renew --cert-name example.com --deploy-hook 'systemctl reload nginx'
sudo openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -noout -dates

若没有 systemd 定时器,可加 cron:0 3 * * * /usr/bin/certbot renew --quiet --deploy-hook 'systemctl reload nginx'。不要日常使用 --force-renewal,否则容易触发重复签发与速率限制。Nginx 插件用户可执行 sudo certbot renew --nginx;webroot 模式需保证 80 端口可访问 /.well-known/acme-challenge/。

实操三:宝塔面板与 acme.sh 续期

宝塔面板在 网站 → 设置 → SSL 页面点续签,续签后确认证书路径通常是 /www/server/panel/vhost/cert/example.com/fullchain.pem,再检查 Nginx 是否加载新文件:

openssl x509 -in /www/server/panel/vhost/cert/example.com/fullchain.pem -noout -dates
nginx -t && nginx -s reload
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates

面板计划任务中应保留自动续签任务,并确认执行日志无报错。若使用 acme.sh,先看列表再续期,并用 --install-cert 把新证书复制到 Web 目录、绑定 reload 命令:

acme.sh --list
acme.sh --renew -d example.com --force
acme.sh --install-cert -d example.com --key-file /etc/nginx/ssl/example.com.key --fullchain-file /etc/nginx/ssl/example.com.crt --reloadcmd 'systemctl reload nginx'
crontab -l | grep acme

通配符证书必须走 DNS API,不能只用 HTTP 文件验证。面板与 acme.sh 都可能存在多份同名证书,务必用线上 openssl s_client 的 serial 与本地文件比对,避免续了旧路径。

配置示例:Nginx、Apache 与自动续期钩子

Nginx 必须指向 fullchain.pem,它包含站点证书与中间证书;只配 cert.pem 会让部分客户端报链不完整。最小可用配置如下:

server {
    listen 443 ssl http2;
    server_name example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
}

Apache 对应配置:

SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

Certbot 部署钩子建议放到 /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh:

#!/bin/sh
set -e
nginx -t
systemctl reload nginx

执行 chmod +x 后,所有续期都会自动校验并重载。商业 OV/EV 证书需先生成 CSR 提交 CA,再下载 fullchain 与私钥;证书最长有效期 398 天,仍应设置到期监控。配置改完先 nginx -t,再 reload,避免直接重启导致业务中断。

对比表格:不同续期方式与适用场景

不同续期方式差异主要在自动化程度、验证方式与适用场景。下表按常见生产环境整理:

方式 典型有效期 自动续期 常用命令/入口 适用场景
Certbot + Let’s Encrypt 90 天 systemd timer 或 cron certbot renew 单机 Nginx/Apache
acme.sh 90 天 cron acme.sh –cron 多域名、DNS API、通配符
宝塔面板 90 天或商业证书 面板计划任务 SSL 页面续签 可视化运维、多站点
商业 OV/EV 最长 398 天 手动或 ACME 网关 CSR 提交、控制台下载 企业合规、品牌展示
自签证书 自定义 手动 openssl req -x509 内网测试,不建议公网

选择建议:能开放 80/443 的单机优先 Certbot;通配符与多云环境优先 acme.sh 的 DNS API;已有面板且站点多可用宝塔,但要验证计划任务与 reload;商业证书适合合规场景,仍需监控到期。无论哪种方式,续期成功不等于生效,必须用 openssl s_client 验证线上证书 serial 与日期。自动续期应至少提前 30 天完成,避免节假日与速率限制导致空窗。

常见错误排查(含真实报错)

下面这些报错在排障中最常见,按现象直接处理:

NET::ERR_CERT_DATE_INVALID
verify error:num=10:certificate has expired
verify error:num=9:certificate is not yet valid
Failed to renew certificate example.com with error: Challenge failed for domain example.com
Problem binding to port 80: Could not bind to IPv4 or IPv6.
nginx: [emerg] cannot load certificate ... BIO_new_file() failed

续期后浏览器仍报日期无效:执行 nginx -T | grep ssl_certificate 看是否仍指向旧路径,openssl s_client 看线上 serial;多个 server 块、默认站点、CDN/负载均衡都可能继续返回旧证书。出现 certificate has expired 说明链中某张证书过期,逐张看 notAfter;出现 certificate is not yet valid 优先查服务器与客户端时间。Challenge failed 多为 80/443 被防火墙拦截、DNS 解析错误或 Nginx 占用端口;standalone 模式需先停 Web 服务,或改用 --nginx、--webroot。BIO_new_file 失败是路径、权限或软链错误,检查 ls -l /etc/letsencrypt/live/example.com/。若报 NET::ERR_CERT_AUTHORITY_INVALID,通常是漏配中间证书,改用 fullchain.pem。

效果验证:浏览器、命令行与监控告警

验证分三层:浏览器、命令行、监控。浏览器先硬刷新,再开无痕窗口;命令行用 curl 看证书校验结果,返回 0 表示链与日期均通过:

curl -sS -o /dev/null -w '%{http_code} %{ssl_verify_result}n' https://example.com
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates -issuer -serial
openssl x509 -checkend 2592000 -noout -in /etc/letsencrypt/live/example.com/fullchain.pem; echo $?

openssl x509 -checkend 2592000 检查 30 天内是否到期,返回 0 表示仍有效,返回 1 表示即将或已过期。生产环境建议加到期监控:Prometheus blackbox_exporter 使用 probe_ssl_earliest_cert_expiry 指标,或在 cron 中每天执行上面的 checkend。最后确认自动任务:systemctl list-timers | grep certbot、crontab -l | grep acme,并查看最近一次续期日志。只有线上证书 serial 变化、日期更新、浏览器无报错,才算真正修复。

常见问题

先用 openssl s_client 看线上证书 notAfter 与 serial,再执行 nginx -T | grep ssl_certificate 确认路径。若文件已新但线上仍旧,通常是 Web 服务未 reload、多个站点块返回旧证书,或 CDN/负载均衡未更新证书。

Let’s Encrypt 证书有效期为 90 天。Certbot 默认在剩余 30 天内自动续期,systemd 定时器每天运行两次。建议保留自动任务并用 certbot renew –dry-run 定期演练,不要等过期后再处理。

检查证书 notBefore 是否在未来,常见原因是签发时服务器时间超前、虚拟机快照恢复或客户端时间错误。用 date -u 与 timedatectl 对比 UTC,必要时用 chronyc makestep 校准后重新签发证书。

dry-run 走 staging 环境,正式续期还受速率限制、DNS 传播、80/443 可达性和 Web 服务占用影响。查看 /var/log/letsencrypt/letsencrypt.log,重点排查 Challenge failed、端口绑定与防火墙规则。

目前公共信任 TLS 证书单张最长有效期为 398 天,Let’s Encrypt 仍为 90 天。行业后续会分阶段缩短,生产环境应以 CA 与浏览器根策略为准,并至少提前 30 天续期。

执行 openssl x509 -in /www/server/panel/vhost/cert/example.com/fullchain.pem -noout -dates 看文件日期,再执行 nginx -t 和 nginx -s reload,最后用 openssl s_client 对比线上 serial 与日期。