解决 OpenSSL 报错 certificate has expired 根证书与中间证书链续签修复
排查 Linux 系统调用或请求外部 API 提示 certificate has expired 与 SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE 错误,更新系统 CA 根证书库。
问题现象
在 Linux 终端使用 curl、wget、Python requests 或 apt-get 访问外部 HTTPS 站点时,操作报错退出:
$ curl https://api.example.com
curl: (60) SSL certificate problem: certificate has expired
More details here: https://curl.se/docs/sslcerts.html
在浏览器中可能显示 SEC_ERROR_EXPIRED_CERTIFICATE 或 SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE。
触发根因
SSL/TLS 证书校验基于自下而上的信任链(Trust Chain):
- 服务器自身证书过期:目标服务端的 SSL 证书超过了有效截止日期;
- 中间证书(Intermediate CA)过期或缺失:服务端在配置 Nginx 时只提供了叶子证书(
cert.pem),缺少完整的证书链(fullchain.pem); - 客户端操作系统的 CA 根证书库陈旧:客户端系统(如旧版 CentOS 7 或 Ubuntu 16.04)内置的公钥基础设施根证书库(如旧版 DST Root CA X3)已经过期,无法信任新签发的权威证书。
解决步骤
1. 查看目标服务端的证书完整链条
使用 OpenSSL 命令行检查远程证书链及有效日期:
openssl s_client -connect api.example.com:443 -showcerts </dev/null
检查输出中每一个证书的 Not After 字段。若服务端叶子证书过期,必须在服务端重新签发更新证书。
2. 在服务端部署完整的 fullchain.pem
在 Nginx 配置中,确保引用的是包含中间证书的完整证书链文件,而非仅包含单个域名的叶子证书:
# 错误写法:
# ssl_certificate /etc/letsencrypt/live/example.com/cert.pem;
# 正确写法:必须使用 fullchain.pem
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
3. 在客户端更新系统 CA 根证书库
如果目标证书在现代系统上正常,但在老旧服务器上报过期,通常是客户端系统根证书陈旧:
在 Debian / Ubuntu 上:
sudo apt-get update
sudo apt-get install --reinstall ca-certificates
sudo update-ca-certificates
在 CentOS / RHEL / Rocky Linux 上:
sudo yum update -y ca-certificates
# 清理废弃的旧根证书
sudo update-ca-trust force-enable
sudo update-ca-trust extract
4. 针对 Python 环境更新 certifi 库
Python 虚拟环境通常自带独立的根证书集合,更新 certifi 包:
pip install --upgrade certifi
验证确认
再次在客户端执行网络请求:
curl -I https://api.example.com
返回 HTTP 200 OK,证书链校验完全通过,certificate has expired 报错彻底解决。


