Deploying Next.js & Laravel (backend) on same server
Unanswered
Silver cyprinid posted this in #help-forum
Silver cyprinidOP
Hello,
I've previously deployed a Next.js app with Laravel as the backend, typically exposing both to the internet. In the Next.js .env file, I added backend_url=https://backend.domain.com, causing the connection to go through the internet even though both are on the same server (NGINX on ports 80 and 443, and Apache on ports 81 and 4443).
For this new project, I don't want to expose backend, and they should communicate locally without going outside, with only the frontend serving content to users.
The issue I'm facing is that when I use http://localhost:81/api/ as the backend_url, I usually get {"message":"fetch failed"} or {"message":"terminated"}. However, if I do curl http://localhost:81/api, I get the desired output, indicating that the backend is working fine. Next.js, however, is not communicating with the backend as it should.
I tried adding NODE_TLS_REJECT_UNAUTHORIZED=0 but still not working
Can someone help me find a solution?
I've previously deployed a Next.js app with Laravel as the backend, typically exposing both to the internet. In the Next.js .env file, I added backend_url=https://backend.domain.com, causing the connection to go through the internet even though both are on the same server (NGINX on ports 80 and 443, and Apache on ports 81 and 4443).
For this new project, I don't want to expose backend, and they should communicate locally without going outside, with only the frontend serving content to users.
The issue I'm facing is that when I use http://localhost:81/api/ as the backend_url, I usually get {"message":"fetch failed"} or {"message":"terminated"}. However, if I do curl http://localhost:81/api, I get the desired output, indicating that the backend is working fine. Next.js, however, is not communicating with the backend as it should.
I tried adding NODE_TLS_REJECT_UNAUTHORIZED=0 but still not working
Can someone help me find a solution?
13 Replies
@Anay-208 can you try deleting `.next` folder?
Silver cyprinidOP
What do you mean by deleting .next folder? And runing again
yarn build ?@Silver cyprinid What do you mean by deleting .next folder? And runing again `yarn build` ?
In your app, there must be a folder called
.next. Delete it, and run dev server, and see if you get the error@Anay-208 In your app, there must be a folder called `.next`. Delete it, and run dev server, and see if you get the error
Silver cyprinidOP
Yes, because when I develop I use
./symfony serve for backend because it has build in https and in the .env.local i set backend_url like this backend_url=https://127.0.0.1:8000/api. I was never able to use just http. I would get the same errorUse http
You can switch in production
Silver cyprinidOP
I don't think you understood the problem. TY 🙂
@Silver cyprinid Hello,
I've previously deployed a Next.js app with Laravel as the backend, typically exposing both to the internet. In the Next.js .env file, I added backend_url=https://backend.domain.com, causing the connection to go through the internet even though both are on the same server (NGINX on ports 80 and 443, and Apache on ports 81 and 4443).
For this new project, I don't want to expose backend, and they should communicate locally without going outside, with only the frontend serving content to users.
The issue I'm facing is that when I use http://localhost:81/api/ as the backend_url, I usually get {"message":"fetch failed"} or {"message":"terminated"}. However, if I do curl http://localhost:81/api, I get the desired output, indicating that the backend is working fine. Next.js, however, is not communicating with the backend as it should.
I tried adding NODE_TLS_REJECT_UNAUTHORIZED=0 but still not working
Can someone help me find a solution?
you meant if you run
fetch("http://localhost:81/api/...") inside the nextjs server, it fails with fetch failed and such?if you make a .js script like
and run it with node in your server, does it work?
// script.js
fetch("http://localhost:81/api/...").then(res => console.log(res.status))and run it with node in your server, does it work?
@joulev you meant if you run `fetch("http://localhost:81/api/...")` inside the nextjs server, it fails with fetch failed and such?
Silver cyprinidOP
yes it fails
this is the whole error catch
I don't know if there is other way to deploy but on my opinion is not necessary the call to backend to be secured because the frontend is secured in the first place and the backend will be accessed directly from the same server
this is the whole error catch
TypeError: terminated
at Fetch.onAborted (node:internal/deps/undici/undici:11000:53)
at Fetch.emit (node:events:513:28)
at Fetch.terminate (node:internal/deps/undici/undici:10272:14)
at Object.onError (node:internal/deps/undici/undici:11095:36)
at Request.onError (node:internal/deps/undici/undici:6477:31)
at errorRequest (node:internal/deps/undici/undici:8440:17)
at Socket.onSocketClose (node:internal/deps/undici/undici:7895:9)
at Socket.emit (node:events:513:28)
at TCP.<anonymous> (node:net:313:12) {
[cause]: SocketError: closed
at Socket.onSocketClose (node:internal/deps/undici/undici:7883:35)
at Socket.emit (node:events:513:28)
at TCP.<anonymous> (node:net:313:12) {
code: 'UND_ERR_SOCKET',
socket: {
localAddress: undefined,
localPort: undefined,
remoteAddress: undefined,
remotePort: undefined,
remoteFamily: undefined,
timeout: undefined,
bytesWritten: 326,
bytesRead: 18397
}
}
}
error { data: { message: 'terminated' } }I don't know if there is other way to deploy but on my opinion is not necessary the call to backend to be secured because the frontend is secured in the first place and the backend will be accessed directly from the same server
@Silver cyprinid yes it fails
this is the whole error catch
`TypeError: terminated
at Fetch.onAborted (node:internal/deps/undici/undici:11000:53)
at Fetch.emit (node:events:513:28)
at Fetch.terminate (node:internal/deps/undici/undici:10272:14)
at Object.onError (node:internal/deps/undici/undici:11095:36)
at Request.onError (node:internal/deps/undici/undici:6477:31)
at errorRequest (node:internal/deps/undici/undici:8440:17)
at Socket.onSocketClose (node:internal/deps/undici/undici:7895:9)
at Socket.emit (node:events:513:28)
at TCP.<anonymous> (node:net:313:12) {
[cause]: SocketError: closed
at Socket.onSocketClose (node:internal/deps/undici/undici:7883:35)
at Socket.emit (node:events:513:28)
at TCP.<anonymous> (node:net:313:12) {
code: 'UND_ERR_SOCKET',
socket: {
localAddress: undefined,
localPort: undefined,
remoteAddress: undefined,
remotePort: undefined,
remoteFamily: undefined,
timeout: undefined,
bytesWritten: 326,
bytesRead: 18397
}
}
}
error { data: { message: 'terminated' } }`
I don't know if there is other way to deploy but on my opinion is not necessary the call to backend to be secured because the frontend is secured in the first place and the backend will be accessed directly from the same server
seems related to this issue https://github.com/nodejs/undici/issues/1412
on my opinion is not necessary the call to backend to be secured because the frontend is secured in the first place and the backend will be accessed directly from the same serveri agree too. you shouldn't have to expose your backend, nor make it https. here it looks weird, i think the root cause is the http/2 shenanigan mentioned by the issue above
Silver cyprinidOP
Is so frustrating maybe I should convert to axios and see if I get the same error