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:
frontend/next.config.jsusesoutput: "standalone".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"
frontend/Procfile:web: node .next/standalone/server.jsfrontend/nixpacks.tomlstart alsonode .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
2 Replies
6 months ago
Hey there! We've found the following might help you get unblocked faster:
- 🧵 502 errors with Cloudflare proxy and custom domain
- 🧵 HTTP 502 Bad Gateway using the $PORT variable assigned by Railway + other service reference $PORT
- 🧵 Persistent SIGTERM Error 502 on Next.js Application Deployment
If you find the answer from one of these, please let us know by solving the thread!
6 months ago
Fix 502 en Next.js standalone (monorepo Railway):
- Railway service (frontend): Root Directory
frontend, Buildnpm ci && npm run build, Startnode .next/standalone/server.js. - Unificar arranque standalone:
package.jsonstart =node .next/standalone/server.jsProcfile=web: node .next/standalone/server.jsnixpacks.tomlstart =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