Fix 502 Bad Gateway after a successful deploy on Coolify
Error: 502 Bad Gateway (Traefik) — build succeeds and container runs, but your-domain.com returns 502 Bad Gateway
Traefik reached your container but couldn't get a response, almost always a port mismatch or the app binding to localhost instead of all interfaces.
Coolify's Traefik proxy routes traffic to the container on the port set in "Ports Exposes". You get a 502 when (a) that port doesn't match the port your app actually listens on, (b) the app binds to 127.0.0.1/localhost so the proxy on the Docker network can't reach it (localhost is reachable only from inside the container), or (c) a host port mapping is set, which interferes with Traefik's routing. In some Coolify betas a missing or malformed Traefik loadbalancer port label causes the same symptom (confirmed in beta.434 and beta.451: the container runs healthy, Traefik logs show no errors but the service has no backend).
The fix
Confirm the port your app listens on, then set it in Coolify under Configuration > General > Ports Exposes to that exact value (e.g. 3000). This is the port Traefik forwards to inside the container, not a host port.
Make the app bind to 0.0.0.0, not localhost/127.0.0.1, so the proxy on the Docker network can connect. This is the single most common cause.
# Node / Express app.listen(3000, '0.0.0.0') # Next.js (production) next start -H 0.0.0.0 -p 3000 # Vite preview vite preview --host 0.0.0.0 --port 3000Remove any host port mappings (Configuration > Network > Ports Mappings) unless you specifically need them. A host mapping interferes with Traefik's routing and can produce a 502. You do not need to publish ports in a Docker Compose file either — Coolify and Traefik handle routing internally. Save and restart.
Redeploy, then confirm the container is actually up.
docker ps --format '{{.Names}}\t{{.Status}}\t{{.Ports}}'If the container is healthy but Traefik still 502s and you're on an affected Coolify beta, inspect the container for a loadbalancer port label and add it manually as a custom label if it's missing. The format Coolify generates is traefik.http.services.<service-id>.loadbalancer.server.port=<container-port> — match the existing <service-id> Coolify uses for this resource, then redeploy.
docker inspect <container-name> --format '{{json .Config.Labels}}' | tr ',' '\n' | grep loadbalancer.server.port
Verify it worked
Loading https://your-domain.com returns your app (HTTP 200) instead of 502, and Traefik logs no longer show "Bad Gateway" or dial errors to the container.
curl -I https://your-domain.comRelated Coolify errors
- Timeout / no response / container not starting
- no available server (Traefik 503) behind your Coolify domain after a successful deploy
- Healthcheck failed / Container is unhealthy — new container never receives traffic, deploy rolls back to the old container (or shows 404 "No available server")
- ECONNREFUSED / ENOTFOUND / connection to server at "postgres" port 5432 failed