2

I have a simple getServerSideProps() function that calls an external API but throws this error:

FetchError: request to https://nginx/api/items failed, reason: unable to verify the first certificate

The Node server does not trust my self-signed certificate.

So I found this Stack Overflow post to bypass that (I'm only using it in development):

How to configure axios to use SSL certificate?

So I added the rejectUnauthorized: false to my Axios call like so:

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

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

But I still get the error.

Is there another way to make my self-signed certificate work with Next? I found some other solutions, but they are for Express, I don't know how to implement that for Node with Next.js

Mureinik
  • 297,002
  • 52
  • 306
  • 350
pileup
  • 1
  • 2
  • 18
  • 45

2 Answers2

3

The rejectUnautorized belongs in an HttpAgent:

const https = require('https');
const agent = new https.Agent({
  rejectUnauthorized: false
});
const res = await fetch('https://nginx/api/items', { 
     method: 'GET',
     agent
   }
);
pileup
  • 1
  • 2
  • 18
  • 45
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

My Case, https created with only key.pem, crt.pem Add ca.pem to https server

Sacru2red
  • 127
  • 1
  • 4