Next.js standalone in monorepo on Railway -> “Application failed to respond” (502) even after fixing start/build
aira10medical
HOBBYOP

6 months ago

Hi Railway folks. I’m stuck with a persistent “Application failed to respond / 502 Bad Gateway” on a Next.js service inside a monorepo.

Repo layout (monorepo):

  • frontend/ = Next.js 14 app (should be the only thing running in this service)
  • root also has FastAPI, but this Railway service is configured to use only frontend/.

Railway service config:

  • Root Directory:frontend
  • Custom Build Command:npm ci && npm run build
  • Custom Start Command:node .next/standalone/server.js
  • Build finishes OK, logs show standalone build + copy step ran.
  • Public domain assigned only to this service.
  • Domain networking shows target port 8080 (Railway UI says setup complete).

What I changed to align standalone mode:

  1. frontend/next.config.js uses output: "standalone".
  2. frontend/package.json:
    • "build": "next build && npm run copy-static"
    • "copy-static": "cp -r .next/static .next/standalone/.next/ && cp -r public .next/standalone/ 2>/dev/null || true"
    • "start": "node .next/standalone/server.js"
  3. frontend/Procfile: web: node .next/standalone/server.js
  4. frontend/nixpacks.toml start also node .next/standalone/server.js

Current behavior:

  • Deploy completes, but the service never responds; any request gives 502 and Railway overlay says “Application failed to respond.”
  • This happens both on the custom domain and on the Railway default domain.

Question:

Given a Next.js standalone app running with node .next/standalone/server.js and Root Directory set to frontend/, what else commonly causes Railway’s edge proxy to fail connecting?

Should I remove Procfile, change target port, or explicitly bind host/port somewhere for standalone?

Any specific log line or setting I should check next?

Thanks

$10 Bounty

2 Replies

Railway
BOT

6 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


aira10medical
HOBBYOP

6 months ago

Fix 502 en Next.js standalone (monorepo Railway):

  • Railway service (frontend): Root Directory frontend, Build npm ci && npm run build, Start node .next/standalone/server.js.
  • Unificar arranque standalone:
    • package.json start = node .next/standalone/server.js
    • Procfile = web: node .next/standalone/server.js
    • nixpacks.toml start = node .next/standalone/server.js
    • build copia assets: .next/static + public.next/standalone/
  • Variable clave Railway: HOSTNAME=0.0.0.0 → Next escucha en 0.0.0.0:$PORT y desaparece el 502.

Luego fix de URLs cruzadas:

  • Abrir dominios públicos: JSON = FastAPI, UI = Streamlit.
  • Variables correctas en frontend:
    • NEXT_PUBLIC_API_URL = <FastAPI>
    • NEXT_PUBLIC_STREAMLIT_URL = <Streamlit>
    • STREAMLIT_URL = <Streamlit> Deploy > done

Welcome!

Sign in to your Railway account to join the conversation.

Loading...