Fix 502 Bad Gateway behind a reverse proxy
A 502 Bad Gateway means the reverse proxy is running but could not get a usable answer from your app, usually because the app is down or on another port or address.
- Updated Nov 27, 2026
- Markdown
Find why a reverse proxy returns 502 and get your app answering again.
- A server with nginx in front of an app
- SSH access with sudo
You deploy, open the site, and see a plain “502 Bad Gateway” page. Your server is up, nginx answers, yet the app does not. Working from the proxy toward the app finds the break in a few commands.
How it works#
A reverse proxy accepts the browser’s request and forwards it to your app, which is called the upstream. The proxy then returns the app’s reply. If the proxy cannot open a connection, or the app closes it without a valid HTTP reply, the proxy tells the browser “502 Bad Gateway”. That page comes from nginx, so you know nginx works.
So a 502 is a question about the hop between nginx and your app. Four things break it:
- The app is not running or crashed.
- The app runs, but on another port or hostname than
proxy_passnames. - The app listens on an address the proxy cannot reach. In a container,
127.0.0.1means “this container only”. - The app accepts the connection and then dies or sends a broken reply.
A 504 is related but different: the proxy reached the app, which did not answer within the proxy’s time limit.
Step 1: Read the proxy error log#
sudo tail -n 30 /var/log/nginx/error.logWhat you should see: lines with the failing request. The line connect() failed (111: Connection refused) while connecting to upstream means nothing is listening at the address in proxy_pass. The line upstream timed out (110: Connection timed out) while reading response header from upstream means a timeout (504).
Step 2: See what is listening#
sudo ss -tlnp-t shows TCP, -l listening sockets, -n numbers instead of names, -p the owning process.
What you should see: one line per listening socket. Find your app’s port. If it is missing, the app is down. If it shows 127.0.0.1:3000 but your proxy points at another port, fix the port in proxy_pass.
Step 3: Test the app directly#
curl -I http://127.0.0.1:3000Replace 3000 with your port. -I fetches headers only.
What you should see: a status line such as HTTP/1.1 200 OK. If this fails with curl: (7) Failed to connect, the proxy is not the problem.
Step 4: Check the container#
docker ps
docker logs --tail 50 NAMEWhat you should see: the container listed as Up. If it is not listed, it exited, and the logs say why. A status like Restarting means it crashes in a loop.
If it runs but is unreachable, check two things. First, the port must be published, for example 127.0.0.1:3000:3000 in your compose file. Second, the app inside must listen on 0.0.0.0, not 127.0.0.1, or the published port cannot reach it. Set the app’s host or bind option, for example HOST=0.0.0.0 or --host 0.0.0.0, depending on the framework.
Check it worked#
Run curl -I https://example.com again. You should get your app’s normal status, such as 200, instead of 502. Then confirm the error log has no new connect() failed lines:
sudo tail -n 5 /var/log/nginx/error.logEdge cases and gotchas#
- Nginx resolves hostnames only at start or reload. If
proxy_pass http://app:3000;points at a container that was recreated with a new IP, nginx may keep the old address. Reload nginx, or use a resolver with a variable. host not found in upstreamat startup. Nginx cannot resolve the name when it reads the config, usually because the proxy and app are not on the same Docker network or the app is not started yet.localhostmay mean IPv6.localhostcan resolve to::1while the app listens on IPv4 only. Use127.0.0.1inproxy_passto be exact.- An app that is still starting. A slow boot gives 502 for the first seconds of a deploy. A health check and a start-up wait avoid sending traffic too early.
- Out of memory kills. If the kernel kills the app on a small server, it disappears with no app log. Check
docker inspect NAME --format '{{.State.OOMKilled}}', and consider adding swap. - SELinux can block the connection. On some Linux distributions that use it, nginx is denied connecting to a port and the log shows
(13: Permission denied). The fix is a policy setting, not a port change. - Behind a CDN. The CDN may show its own 502 or 5xx page. Check whether the error comes from the CDN or from your server by calling your origin address directly.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
connect() failed (111: Connection refused) while connecting to upstream | App down or wrong port in proxy_pass | Start the app, or fix the port, then sudo nginx -t and reload |
no live upstreams while connecting to upstream | All servers in an upstream block are marked failed | Fix the app, then reload nginx |
upstream prematurely closed connection while reading response header from upstream | App crashed or reset the connection mid-request | Read the app logs for a crash or exception |
upstream timed out (110: Connection timed out) (504) | App too slow or hung | Find the slow request, or raise proxy_read_timeout as a stopgap |
| Works after restart, then 502 again | App crashes under load or runs out of memory | Check logs, OOMKilled, and memory use |
| 502 only inside Docker, app works on the host | App listens on 127.0.0.1 inside the container | Bind to 0.0.0.0 |
(13: Permission denied) while connecting to upstream | SELinux or a firewall rule blocks nginx | Allow the connection in the policy |
| Still 502 after editing the config | Config not reloaded | sudo nginx -t then sudo systemctl reload nginx |
Real-world scenario#
You move an app into Docker. Before, it ran directly on the server on port 3000 and nginx used proxy_pass http://127.0.0.1:3000;. After the move the site returns 502.
sudo tail -n 3 /var/log/nginx/error.log
docker ps
curl -I http://127.0.0.1:3000The log shows connect() failed (111: Connection refused) while connecting to upstream. docker ps shows the container is up with no ports column entry, and curl fails. The compose file has no ports: line, so nothing is published. You add 127.0.0.1:3000:3000, run docker compose up -d, and curl now answers, but with Connection reset by peer. The app is binding to 127.0.0.1 inside the container. You set its host option to 0.0.0.0, recreate the container, and the site loads. Two separate causes hid behind one symptom, which is why each step checks one hop.
Quick reference#
| Check | Command |
|---|---|
| Proxy error log | sudo tail -n 30 /var/log/nginx/error.log |
| Listening ports | sudo ss -tlnp |
| Test the app | curl -I http://127.0.0.1:3000 |
| Container state | docker ps and docker logs --tail 50 NAME |
| Test and reload nginx | sudo nginx -t && sudo systemctl reload nginx |
| OOM check | docker inspect NAME --format '{{.State.OOMKilled}}' |
Related lessons#
- Read container logs
- What is a reverse proxy?
- Publish container ports safely
- Container restarting in a loop
Next#
Protect your data before the next outage: Back up a Postgres database in Docker.
Check what you learned
Frequently asked questions
Is 502 an nginx problem?
Rarely. Nginx is working, since it answered you. The fault is almost always the app behind it, which may be stopped, crashed, or not where the proxy expects.
Why does my container app refuse connections?
If it listens on 127.0.0.1 inside the container, nothing outside can reach it. It must listen on 0.0.0.0.
What is the difference between 502 and 504?
502 means the proxy got no valid answer, often a refused connection or a crashed app. 504 means the app was reached but did not answer in time.
The app works with curl on the server but not through nginx. Why?
The proxy may point at another port or hostname, run in a different network or container, or be blocked by a security policy such as SELinux.
Sources
Keep going: guides and tutorials
- Zero Downtime Deploy for Docker Apps, With Rollback How a Docker deploy waits for readiness before cutover, holds the previous release for rollback, rolls back on its own, and how to rehearse it.
- Roll Back a Failed Docker Deploy Without Downtime Ship a broken release on purpose, watch your users stay on the old version, then roll back with one command. Real commands, real output, self-hosted.
- Our Health Check Failed and the Deploy Went Live Anyway A postmortem from our own dogfood run: a failed readiness probe still received traffic. What broke, why two correct parts added up to a bug, and the fix.