Replit published app shows 502 Bad Gateway or "Application failed to respond" after republishing
Preview works, you click Republish, and the live replit.app URL returns 502 or never finishes publishing. It is usually the server crashing at startup, listening on the wrong host or port, or a missing production secret. How to read the logs and fix each one.
Free · no card
Paste your error. Get a free diagnosis by email.
Tell us what broke and we’ll email you a short diagnosis: the likely cause and what to try first. Usually within a day.
What you are seeing
- Your
.replit.appURL or custom domain returns502 Bad Gatewayright after a republish, while Preview in the editor still works. - The page says
Application failed to respond, or publishing fails at the last step after the build succeeded. - It worked yesterday; the agent changed something small (a new package, a new env var, a port) and now the published version is down.
- The 502 comes and goes: first loads fail, or it dies after some traffic.
Why it happens
- The app crashes during startup in production. Replit's docs say a 502 usually means the process crashed, and unhandled promise rejections or uncaught exceptions can kill a Node.js process without an obvious message.
- A secret is missing in the published app. Replit says secrets set in the editor do not automatically carry over to the published app, so code that reads them at startup crashes (database URL, Stripe or Supabase keys).
- The server listens on
localhostor127.0.0.1, or on a hard-coded port. Published apps need the server on0.0.0.0, and Replit passes the expected port in thePORTenvironment variable. - A
[[ports]]entry in.replitturns off automatic port detection. Then the mapping withexternalPort = 80must point at thelocalPortyour server actually uses. - Startup is too slow. Replit health-checks the homepage before going live, and if it takes more than about five seconds to respond, publishing can fail. Running migrations, seeding, or a big build in the run command is a common cause.
- The run command is a one-off script or a dev server that exits, rather than a long-running production server.
How to fix it
1. Check status, then read the deployment logs first
Check status.replit.com for an incident. Then open Publishing > Logs and scroll to the moment of the failed publish or the first 502. Find the first real error (a stack trace, "Cannot find module", "is not defined", "ECONNREFUSED", a missing env var), not the final "failed" line. Save the logs before republishing.
2. Bind to 0.0.0.0 and the PORT variable
// Node / Express
const PORT = Number(process.env.PORT) || 3000
app.listen(PORT, '0.0.0.0', () => console.log(`listening on ${PORT}`))
# Python / Flask
port = int(os.environ.get('PORT', 3000))
app.run(host='0.0.0.0', port=port)
# Vite preview / Next.js start in the run command
vite preview --host 0.0.0.0 --port $PORT
next start -H 0.0.0.0 -p $PORT
3. If .replit has ports, make the mapping match
# .replit (only if you have a [[ports]] section; it disables auto-detection)
[[ports]]
localPort = 3000 # the port your server really listens on
externalPort = 80
# Remove stale [[ports]] entries left over from earlier agent attempts.
4. Copy every secret into the published app
# list what the code reads, then compare with Publishing > Secrets
grep -rhoE "process\.env\.[A-Z0-9_]+|os\.environ(\.get)?\[?\(?['\"][A-Z0-9_]+" \
--include=*.js --include=*.ts --include=*.py --exclude-dir=node_modules . | sort -u
# Add any missing ones in the Publishing pane, then republish.
5. Surface crashes and fail loudly on missing config
// top of your server entry file
process.on('uncaughtException', (err) => console.error('Uncaught:', err))
process.on('unhandledRejection', (err) => console.error('Unhandled rejection:', err))
for (const k of ['DATABASE_URL', 'SESSION_SECRET']) {
if (!process.env[k]) console.error(`Missing env var ${k}`)
}
6. Make startup fast and the run command long-running
Put the build in the build command and only start the server in the run command (for example your package.json build script in the build command and its start script in the run command). Move migrations and seeding out of server startup or make them quick and idempotent, so the homepage answers within a few seconds. Do not use a dev server (the dev script) as the production run command.
7. If it dies under traffic, check Monitoring
Open Monitoring for the deployment and compare the 502 times with CPU and memory. Memory climbing to the limit before each crash points at a leak or loading too much data per request; a bigger machine only buys time.
How to know it is fixed
Publishing completes, the .replit.app URL and any custom domain load on the first try (allow a few seconds for a cold start on Autoscale), the logs show your "listening on" line with no stack traces, and the main flow (sign in, load data, pay) works at the public URL. Sources: Replit: Networking and app errors (502, Application failed to respond), Replit: Troubleshoot deployments, and Replit: Troubleshoot publishing (0.0.0.0, ports, secrets, health check). Related: Environment variables undefined after deploy and Blank white screen after deploy.