2 months ago
Dear Railway Support Team, I hope you are doing well. I am writing to request assistance regarding an issue I am experiencing with my application hosted on Railway.
I have set up the backend of the application using NestJS and the frontend using Next.js. I have also attached a screenshot showing the folder structure of my application for your reference. - https://prnt.sc/KLTiMfuaugJP
Issue Details:
Error Code: 502
Error Message: Application failed to respond
Application URL: https://cfd-platform-backend-production.up.railway.app/
4 Replies
2 months ago
Hi there!
A 502 "Application failed to respond" error usually means Railway's proxy can't reach your application. For NestJS, this typically happens when your app isn't listening on the right host and port combination.
Make sure your NestJS app is configured to listen on 0.0.0.0 and uses the PORT environment variable that Railway provides. In your main.ts, update your bootstrap function to grab the port from the environment and bind to the correct host:
const port = process.env.PORT || 3000;
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// ... your other configuration
await app.listen(port, "0.0.0.0");
}
bootstrap();The key thing here is that the host needs to be 0.0.0.0 (not localhost or 127.0.0.1), and the port should come from the environment variable. Once you've made this change, redeploy and the error should clear up.
If you've already got this set up correctly, double-check your service's Settings → Networking section to make sure the target port isn't misconfigured, and take a look at your deploy logs to confirm the application is starting without errors.
Status changed to Awaiting User Response Railway • 2 months ago
2 months ago
I have updated the code as per your comments; however, I am still encountering a 502 error (“Application failed to respond”). Please review the attached screenshot for reference. - https://prnt.sc/lE0sydzv436O
Additionally, I have shared another screenshot showing a workspace error on Railway after the build deployment. Kindly check and let me know your thoughts. - https://prnt.sc/egxEqQMeyU-P
Status changed to Awaiting Railway Response Railway • 2 months ago
2 months ago
this isn’t a nestjs port issue anymore. your screenshots show a workspace/monorepo misconfiguration
railway is building the whole repo, but your backend is in a subfolder, so the server never actually starts → proxy returns 502
so follow this :
go to your backend service → settings
set root directory to the backend folder (the one with
package.json+ nest app)OR explicitly set a start command, e.g.
npm run start(from backend folder)
ornpm --workspace backend run start
redeploy
confirm logs show
nest application successfully started
also double-check settings → networking → target port matches process.env.PORT.
when the app actually starts, the 502 will disappear.
2 months ago
I have already configured the backend as the root for the builder deployment. Please refer to the attached screenshot for confirmation.
I have also attached the package.json file from the root folder. Please review it here.
Here is my backend NestJS package.json file. Please review it.
Here is my frontend NextJS package.json file. Please review it.