Nodejs应用报错SSL验证失败 reason: unable to verify the first certificate
· 技术积累 · Nodejs SSL错误

部署的开源chatgpt\nodejs应用,今天使用时候发送消息一直返回域名的ssl验证错误 UNABLE_TO_VERIFY_LEAF_SIGNATURE

应用1: https://github.com/Chanzhaoyu/chatgpt-web
应用2: https://github.com/Yidadaa/ChatGPT-Next-Web
node版本: v16.18

错误截图

Nodejs应用报错SSL验证失败 reason: unable to verify the first certificate
Nodejs应用报错SSL验证失败 reason: unable to verify the first certificate

我使用的是acme.sh申请的免费证书,浏览器访问此域名,ssl验证也是没问题的,如果使用curl跳过ssl检测直接提交请求也正常

Nodejs应用报错SSL验证失败 reason: unable to verify the first certificate
Nodejs应用报错SSL验证失败 reason: unable to verify the first certificate

排查原因:应用之前都是好的,且没有修改代码,但是修改过代理域名地址(用curl直接请求成功,说明代理是可以用的)

1、ssl证书: 使用的zerossl颁发的免费证书,更换为Let's Encrypt颁发的证书,还是同样的错误【如果使用云厂商签发的免费ssl,应该是没问题的】
2、nodejs: 测试Node.js代码来检查CA机构的支持发现是nodejs无法验证ssl证书导致的,应该是我使用的CA签发的证书有问题,证书链不完整 nodejs文档-X509 证书的错误码

测试代码

const https = require('https');

const options = {
  hostname: 'example.com', // 你要测试的主机名
  port: 443, // HTTPS端口
};

const req = https.get(options, (res) => {
  // 这里不需要做实际请求,只需建立连接即可
});

req.on('error', (error) => {
  if (error.code === 'DEPTH_ZERO_SELF_SIGNED_CERT') {
    console.log('CA机构不被支持');
  } else {
    console.error('发生错误: ', error);
  }
});

Nodejs应用报错SSL验证失败 reason: unable to verify the first certificate

解决方法:取消对ssl的验证,参考python&nodejs应用https代理抓包证书验证问题解决方法

我node也就这一个应用在用,所以直接修改系统环境变量,环境变量生效后重启下服务,就正常了

# 我的系统ubuntu:,在系统环境变量中配置NODE_TLS_REJECT_UNAUTHORIZED=0
sudo nano /etc/environment

# 文件末尾添加:
NODE_TLS_REJECT_UNAUTHORIZED=0

本文最后更新时间 2023-11-25
文章链接地址:
https://wojc.cn/archives/1461.html
本站文章除注明[转载|引用],均为本站原创内容,转载前请注明出处

NextTrace 一款轻量化开源可视路由跟踪工具

M3U8流媒体视频在线解析下载器

我要留言