Next.js Discord

Discord Forum

How could I get around the self-signed certificate error?

Answered
American Curl posted this in #help-forum
Open in Discord
Avatar
American CurlOP
Hi, I switched to connect to the backend server which has a self-signed certificate. Then the Error: self-signed certificate error occurred. How could I get around it? This is how I configured to use it in next.config.mjs:
async rewrites() {
  return [
    {
      source: '/api/:path',
      destination: 'https://mydomain/api/:path'
    }
  ]
}

It worked with the HTTP server, so the error implied that it was about the HTTP certificate. So how could I get around it in development? Thanks.
Answered by Anay-208
Hmm, why are you rewriting requests, why don't you directly send to mydomain
View full answer

32 Replies

Avatar
So If I'm right, you are rewriting a request to a different website.

Is the mydomain hosted by you?
Avatar
American CurlOP
yes. I just upgraded to use HTTPS which has a self-signed certificate.
Avatar
Hmm, why are you rewriting requests, why don't you directly send to mydomain
Answer
Avatar
American CurlOP
There's CORS issue if I send to mydomain, so this avoids fixing the CORS issue😅
Avatar
fix the CORS issue then, use npmjs package cors
Avatar
American CurlOP
It's been deployed by the backend developer that the API and the frontend portal in the same nginx.
Avatar
American CurlOP
Thanks, I will check out.
Avatar
It needs to be added to the backend
With this approach, it might use more memory rewriting requests
Once your issue is resolved, mark solution please
Avatar
American CurlOP
How can I mark it?
I see.
Are there any other solutions?
I think this should be common case using rewrites to redirect to the backend.
It's like a proxy.
Avatar
No, it is only if an api key is being passed
or some sensitive info, which shouldn't be shown to client
Avatar
American CurlOP
I searched online. There're some solutions like adding NODE_TLS_REJECT_UNAUTHORIZED=0, but none is workign.
Is this used in backend to fix the CORS issue?
Avatar
yes
even if you post on stackoverflow, your post will get -1 since this is not a good approach
Avatar
@American Curl Let me know if you need any further help regarding this.

If you want to proxy through your nextjs app(NOT RECOMMEND AT ALL), a approach would be to:
- create a api route, /api/[...slug]/route.ts
- Get the slug, and send the request to domain/slug, with same body & headeres
Avatar
American CurlOP
Thanks. I just started using this. And I didn't use this feature yet.
We just switched to Static Exports to build the application, so I'm not sure if it's supported feature.
Avatar
it won't work then
With static
Avatar
American CurlOP
Meantime, I'm talking with backend guys to help fix CORS issue from backend then I can access the API directly.
I'm asking to make sure if there are solutions for the rewrites case.
Avatar
Ping me if you've any more queries
in a different thread
Avatar
American CurlOP
Great, thanks.