我有一个简单的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
rejectUnautorized
属于HttpAgent
:
const https = require('https');
const agent = new https.Agent({
rejectUnauthorized: false
});
const res = await fetch('https://nginx/api/items', {
method: 'GET',
agent
}
);
我的情况下,https只使用密钥创建。pem, crt.pem添加ca.pem到https服务器