Application failed to respond 502
hussamkhatib
HOBBYOP

2 months ago

Error Request Id: Sh0rvm92QVGo1K1Bmrpb1w

I moved my Next.js application from Netlify to railway

Below are some of the details

My start command

"node .next/standalone/server.js"
// next.config.js
const config = {
    output: "standalone",
};

I'm not sure what target port I should be using, I tried with both 3000 and 8080. In Settings as well as defined in Env variables

Additional Info

App starts on the below hostname, does it absolutely have to be 0.0.0.0

- Local: http://dd34c36d0579:8080

- Network: http://dd34c36d0579:8080

Solved$10 Bounty

Pinned Solution

domehane
FREE

2 months ago

Hello hussam,

the missing styles/scripts is a classic standalone issue. next.js doesn't automatically copy your static assets into the standalone folder, you need to do it manually in your build command:

next build && cp -r .next/static .next/standalone/.next/static && cp -r public .next/standalone/public

and your start command stays as : HOSTNAME=0.0.0.0 node .next/standalone/server.js

that's it, the html was rendering because the server works fine, it just couldn't find the css and js files

Hope this help you :)

5 Replies

Railway
BOT

2 months ago

Your logs confirm the app is binding to hostname dd34c36d0579 instead of 0.0.0.0, which means our proxy cannot reach it, causing the 502. Next.js standalone requires HOSTNAME=0.0.0.0 to be set. Update your start command to HOSTNAME=0.0.0.0 node .next/standalone/server.js, remove any manually set PORT variable so it uses the one we inject automatically, and ensure your domain's target port is not set (or matches the port shown in your logs). See our troubleshooting guide for more details.


Railway
BOT

2 months ago

This thread has been marked as private. Any further activity in this thread will only be visible to you and Railway employees.

Status changed to Awaiting User Response Railway about 2 months ago


Status changed to Awaiting Railway Response Railway about 2 months ago


hussamkhatib
HOBBYOP

2 months ago

Update: There seems some issue with Next.js itself

Right now, all I see is html in the webpage.

I have given all the details regarding my setup, if you need to know any additional info, do let me know

Attachments


Railway
BOT

2 months ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open Railway about 2 months ago


domehane
FREE

2 months ago

Hello hussam,

the missing styles/scripts is a classic standalone issue. next.js doesn't automatically copy your static assets into the standalone folder, you need to do it manually in your build command:

next build && cp -r .next/static .next/standalone/.next/static && cp -r public .next/standalone/public

and your start command stays as : HOSTNAME=0.0.0.0 node .next/standalone/server.js

that's it, the html was rendering because the server works fine, it just couldn't find the css and js files

Hope this help you :)


andreahlert
PRO

2 months ago

hey! this is a classic Railway 502 and it’s almost certainly the hostname binding issue.

yes, it absolutely has to be 0.0.0.0.

Railway’s proxy needs your app to be available at host 0.0.0.0. that hostname you’re seeing (dd34c36d0579) is the container’s internal hostname, which means your server is only listening on that container’s loopback interface and Railway’s edge proxy can’t reach it.

the fix for standalone Next.js: you need to set the HOSTNAME environment variable in Railway to 0.0.0.0. the standalone server.js reads from process.env.HOSTNAME.

so in your Railway service variables, add:

HOSTNAME=0.0.0.0

PORT=3000

then your start command stays the same:

node .next/standalone/server.js

the standalone server will pick up both env vars automatically. also make sure the target port under your public domain settings matches the port your app listens on (so if PORT=3000, your domain target port should also be 3000, or you can just leave target port empty and Railway will auto detect it)o.

you do NOT need to hardcode the port in Settings if you’re setting PORT in env vars. pick one approach and stick with it so they don’t conflict.


domehane

Hello hussam, the missing styles/scripts is a classic standalone issue. next.js doesn't automatically copy your static assets into the standalone folder, you need to do it manually in your build command: next build && cp -r .next/static .next/standalone/.next/static && cp -r public .next/standalone/public and your start command stays as : HOSTNAME=0.0.0.0 node .next/standalone/server.js that's it, the html was rendering because the server works fine, it just couldn't find the css and js files Hope this help you :)

hussamkhatib
HOBBYOP

2 months ago

Got it, thanks I was missing .next/static .next/standalone/.next/static && cp -r public .next/standalone/public as well


Status changed to Solved sam-a about 2 months ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...