curl 报 certificate verify failed: unable to get local issuer certificate:排查中间证书缺失
站点在桌面浏览器正常打开,但在服务器执行 curl 或代码调用时抛出 SSL 证书校验失败。分析服务端配置缺少中间证书(Fullchain)的根因,并给出使用 OpenSSL 验证与合成证书链的操作步骤。
故障现象
网站配置 HTTPS 证书后,使用 Chrome/Safari 桌面浏览器访问显示安全锁,一切正常。
但是在 Linux 服务器上使用 curl 请求该接口,或在 Node.js/Python 脚本中发起 HTTPS 请求时,却抛出证书校验失败异常:
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it.
在 Python requests 中调用报出 requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED]。
核心根因
核心根因在于 Web 服务器(如 Nginx、Caddy、Apache)只配置了终端实体证书(Leaf/Server Certificate),缺失了中间 CA 证书(Intermediate CA Certificate),导致证书链不完整。
- 证书信任链机制:操作系统的本地信任库中只内置了最顶层的根证书(Root CA)。中间 CA 由根 CA 签名,而我们申请的域名证书由中间 CA 签名。客户端验证时,必须沿着
域名证书 -> 中间证书 -> 根证书逐级推导验签。 - 为什么桌面浏览器没报错:现代桌面浏览器具有 AIA 抓取功能(Authority Information Access),如果在握手时服务端没提供中间证书,浏览器会根据证书内的 URL 自动向外网请求下载缺失的中间 CA;同时浏览器还会复用其他网站已经缓存的中间证书。
- 命令行与标准库的严格拦截:Linux 下的
curl、openssl、Python 以及 Java 等运行时不会自动去公网下载中间证书。如果服务端没有主动发送完整的证书链(Fullchain),客户端由于找不到签发者,直接判定证书非法并中止握手。
排查步骤
1. 使用 openssl 工具查看服务端提供的证书链深度
在终端执行命令,直接打印服务端实际握手提供的证书信息:
openssl s_client -connect yourdomain.com:443 -servername yourdomain.com
查看输出中开头的 Certificate chain 部分:
缺陷情况(仅提供 1 张证书,缺少中间证书):
Certificate chain
0 s:CN = yourdomain.com
i:C = US, O = Let's Encrypt, CN = R3
此时会紧接着打印 Verify return code: 21 (unable to verify the first certificate)。
正常情况(提供了完整的逐级证书链):
Certificate chain
0 s:CN = yourdomain.com
i:C = US, O = Let's Encrypt, CN = R3
1 s:C = US, O = Let's Encrypt, CN = R3
i:C = US, O = Internet Security Research Group, CN = ISRG Root X1
输出显示 Verify return code: 0 (ok)。
解决办法
方法一:合并中间证书构建完整 Fullchain
获取申请证书时机构提供的两份文件:
yourdomain.crt(你的域名实体证书)intermediate.crt(中间 CA 证书,部分机构命名为 bundle/ca.crt)
将它们合并为一个完整的 fullchain.crt 文件。必须注意顺序:自己的域名证书必须放在最上面,中间证书追加在后面:
cat yourdomain.crt intermediate.crt > fullchain.crt
验证合并后的文件格式:
openssl crt -in fullchain.crt -text -noout
方法二:正确配置 Nginx 证书路径
在 Nginx 的配置文件中,将 ssl_certificate 指向合并后的 fullchain.crt,而不是单张实体证书:
server {
listen 443 ssl http2;
server_name yourdomain.com;
# 必须指向包含域名证书与中间证书的 fullchain 文件
ssl_certificate /etc/nginx/ssl/fullchain.crt;
ssl_certificate_key /etc/nginx/ssl/yourdomain.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
}
测试配置语法并重载 Nginx:
sudo nginx -t && sudo nginx -s reload
方法三:Certbot / ACME 自动化证书注意事项
如果使用 Certbot 自动化申请,请确保在 Web 服务器中引用的是 fullchain.pem,而不是 cert.pem:
- ✅ 正确:
ssl_certificate /etc/letsencrypt/live/yourdomain/fullchain.pem; - ❌ 错误:
ssl_certificate /etc/letsencrypt/live/yourdomain/cert.pem;
验证恢复
在纯净的 Linux 机器上使用 curl 再次验证:
curl -Iv https://yourdomain.com
确认返回 SSL certificate verify ok 并顺利输出 HTTP/2 200 响应。


