首页 > 解决方案 > FetchError:无法验证第一个证书,但我添加了rejectUnauthorized:false

问题描述

我有一个getServerSideProps()调用外部 API 但抛出此错误的简单函数:

FetchError:对 https://nginx/api/items 的请求失败,原因:无法验证第一个证书

Node 服务器不信任我的自签名证书。

所以我发现这篇 Stack Overflow 帖子绕过了它(我只在开发中使用它):

如何配置 axios 使用 SSL 证书?

所以我rejectUnauthorized: false像这样添加到我的 Axios 调用中:

export async function getServerSideProps() {
const res = await fetch('https://nginx/api/items',
   { rejectUnauthorized: false,
     method: 'GET',
   }
)

const { data } = await res.json()
return { props: { data } }
}

但我仍然得到错误。

还有其他方法可以让我的自签名证书与 Next 一起使用吗?我找到了其他一些解决方案,但它们是针对 Express 的,我不知道如何使用 Next.js 为 Node 实现它

标签: javascripthttpsaxiosnext.js

解决方案


rejectUnautorized属于HttpAgent:_

const https = require('https');
const agent = new https.Agent({
  rejectUnauthorized: false
});
const res = await fetch('https://nginx/api/items', { 
     method: 'GET',
     agent
   }
);

推荐阅读