# Replit app shows 502 Bad Gateway after republishing (or publish fails at the last step)

> Canonical page (link this when citing): https://unbreakmyapp.vercel.app/fixes/replit-publish-502-bad-gateway-after-republish.html
> Category: Deploys | Tools: Replit | Published: 2026-11-22 | Updated: 2026-11-22

## The error

My published replit.app URL (or custom domain) returns 502 Bad Gateway right after I republished / Preview works but the live app is a 502 / publishing gets stuck or fails at the final Promote step / the app was fine yesterday and broke after the Agent's last change and a republish

## Why it happens

A 502 Bad Gateway means a proxy in front of your app did not get a valid response from the server behind it (MDN). On Replit that server is your own app process, so a 502 after a republish almost always means the new version is not answering: it crashed on startup, it is listening on the wrong address or port, or it is too slow to respond. Replit's troubleshooting guide lists the usual causes. The server must listen on 0.0.0.0 (localhost or 127.0.0.1 does not work for published apps). If you set ports in .replit, automatic port detection is turned off and one port needs externalPort = 80, and Autoscale and Reserved VM apps can only expose a single external port. The app must keep running (a script that runs once and exits gets restarted). Before switching the live URL to a new version, Replit sends it an HTTP request, and if your homepage takes more than five seconds to respond the health check can time out and the publish fails at the final step. A very common trigger in vibe-coded apps is an Agent change that only works in the editor: a new environment variable or secret that the published app does not have, a database call at startup that fails against the production database, or a new dev server command. Replit's docs also say the published app's file system resets on every publish, so files the app wrote to disk are gone. Replit does not give you a one-click rollback of a published version: its docs say you are responsible for tracking tested, healthy releases (each publish creates a Published your App commit and the deploy logs show the Build ID and Deployment ID, kept for 30 days).

## How to fix it

1. Confirm it is your app and not your network. Open the live URL in a private window and on mobile data. Replit's docs note that web filters or DNS can block replit.app and replit.dev domains; if only one network fails, try a public DNS resolver such as 1.1.1.1 or 8.8.8.8. If it fails everywhere, carry on.
2. Check that Preview still works. Select Run and open Preview. If Preview is broken too, publishing will not fix it: fix the app first. If Preview works and only the published app gives 502, the problem is publishing-specific.
3. Read the deploy logs before changing anything. Open Publishing and select View logs (or Tools > Publishing > Current status > View deploy logs), and look at the Logs in Monitoring with Errors only switched on. Look for a stack trace, 'Cannot find module', a missing variable, a database connection error or the process exiting. Monitoring's HTTP Statuses chart shows when the errors started, which tells you which publish caused them.
4. Check host and port. The server must listen on 0.0.0.0, not localhost or 127.0.0.1, and should use the port Replit gives it (process.env.PORT with a fallback). Open .replit: if it has [[ports]] entries, automatic port detection is off, so the port your server listens on must be the localPort of an entry with externalPort = 80. Autoscale and Reserved VM can only expose one external port, so remove extra exposed ports (for example a second dev server or a websocket port added by the Agent).
5. Check the run and build commands in the Publishing settings. A dev command (vite, next dev, npm run dev) or a wrong entry file often works in Preview but not when published. Use the production build command and a start command that starts a long-running web server.
6. Check production secrets. In Publishing, select Adjust settings and look at Production app secrets. Make sure every key the new code reads (database URL, Supabase, Stripe, OpenAI, auth secrets) is there with the production value. Replit's pages describe secret syncing differently, so check the actual values rather than assuming they copied over. An undefined secret read at startup crashes the server, which shows up as a 502.
7. Make the homepage answer fast. Replit's health check gives your homepage about five seconds. Do not run migrations, seed data, warm caches or call slow APIs before the server starts listening. Start the server first, keep the / route light, and move heavy work behind a user action or a background job.
8. Check the deployment type. If the app has API routes, logins, webhooks or server-side database calls, it needs Autoscale or Reserved VM, not Static. Static only serves files, so the backend parts will fail.
9. Go back to a known-good version if you cannot fix it quickly. There is no one-click deployment rollback, so roll back to the Agent checkpoint or Git commit from your last working publish (the Published your App commits in the Git pane help you find it) and publish again. Be careful: rolling back code does not restore production data. Then reapply the new change in small steps, checking Preview and the logs each time.
10. After it works, republish and verify the live URL, not Preview: load the homepage, log in, and run the main action. Note the Deployment ID and Git commit of this healthy release so you have a rollback point next time.

## Error codes this fixes

- `502 Bad Gateway on the published replit.app URL after republishing` (Replit published app): https://unbreakmyapp.vercel.app/errors.html#502-bad-gateway-on-the-published-replit-app-url-after-republ
- `Publishing fails at the final Promote step (health check)` (Replit Publish): https://unbreakmyapp.vercel.app/errors.html#publishing-fails-at-the-final-promote-step-health-check

## Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

```text
My Replit app works in Preview but the published URL returns 502 Bad Gateway (or publishing fails at the final step) since my last republish. Do not change app features, do not touch production data and do not disable any security checks. 1) Read the latest deploy logs and the published app's error logs and tell me the first real error. 2) Check that the server listens on host 0.0.0.0 and on process.env.PORT with a fallback, not localhost or a hard-coded port. 3) Check .replit: if it has [[ports]] entries, make sure exactly one is exposed with externalPort = 80 and its localPort matches the port the server listens on. 4) Check the publishing build and run commands use the production build and a start command that runs a long-running server, not a dev server. 5) List every environment variable and secret the code reads, and tell me which ones I must add or check under Publishing > Adjust settings > Production app secrets. Do not print secret values. 6) Make sure GET / responds in well under five seconds: start listening before any migrations, seeding or slow API calls. 7) Confirm the deployment type is Autoscale or Reserved VM if the app has a backend. Explain each change in one line and give me a short checklist to verify the live URL after I republish.
```

## FAQ

### What does 502 Bad Gateway mean on a Replit app?

It means Replit's proxy could not get a valid response from your app's server. After a republish that usually means the new version crashed on startup, is listening on the wrong address or port, or is too slow to answer. Check the deploy logs first.

### Why does my Replit app work in Preview but not when published?

Preview and the published app run differently. The published app needs to listen on 0.0.0.0, use a single exposed port, use production run and build commands and have its own production secrets, and its file system resets on every publish. Replit's troubleshooting guide covers each of these.

### Why does Replit publishing fail at the last step?

Before switching your URL to the new version, Replit sends it a request. If the homepage takes more than about five seconds, or the server is not listening on the expected port, the check fails and the publish stops at the final Promote stage. Start the server first and keep the / route fast.

### Can I roll back a Replit deployment to the previous version?

Not with one click. Replit says you are responsible for tracking tested, healthy releases. Roll back the code to the checkpoint or Git commit from your last good publish (look for Published your App commits) and publish again. Rolling back code does not change your production database.

### Could the 502 be Replit's fault?

Sometimes, but check your own app first: if Preview works and the deploy logs show your server crashing or not listening, it is the app. If the logs look healthy, the app fails on every network and nothing changed, check Replit's status page and contact Replit Support with your Deployment ID.

## Sources (official documentation)

- [Replit: Troubleshoot publishing](https://docs.replit.com/build/troubleshooting)
- [Replit: After you publish (stages, deploy logs, rollback points)](https://docs.replit.com/features/publishing/after-you-publish)
- [Replit: Monitoring your app (logs, HTTP statuses)](https://docs.replit.com/features/publishing/monitoring-a-deployment)
- [Replit: Ports (0.0.0.0, externalPort 80, single port)](https://docs.replit.com/features/project-setup/ports)
- [Replit: Secrets (production app secrets)](https://docs.replit.com/core-concepts/project-editor/app-setup/secrets)
- [Replit: Publishing overview (deployment settings)](https://docs.replit.com/features/publishing/overview)
- [Replit: Deployment types](https://docs.replit.com/features/publishing/deployment-types)
- [Replit: Checkpoints and Rollbacks](https://docs.replit.com/features/version-control/checkpoints-and-rollbacks)
- [MDN: 502 Bad Gateway](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/502)

## Related fixes

- [Replit deploy: app not reachable / port not open](https://unbreakmyapp.vercel.app/fixes/replit-port-not-open.html)
- [Environment variable is undefined in production](https://unbreakmyapp.vercel.app/fixes/env-var-undefined-in-production.html)
- [App works on localhost but breaks in production: a checklist to find the cause](https://unbreakmyapp.vercel.app/fixes/works-on-localhost-breaks-in-production.html)
- [Replit rollback did not bring my data back: how to restore the development or production database](https://unbreakmyapp.vercel.app/fixes/replit-rollback-does-not-restore-database.html)
- [Postgres: too many connections / remaining connection slots are reserved](https://unbreakmyapp.vercel.app/fixes/supabase-too-many-connections.html)

---
Unbreak My App: free fixes for broken vibe-coded apps. https://unbreakmyapp.vercel.app/
