Some links on this page are affiliate links. If you sign up through one, Rigcheck may earn a commission at no cost to you. It doesn't change what this guide recommends. How we handle this.
A 502 Bad Gateway error means one server tried to get a page from another server on your behalf, and got back nothing usable. The server showing you the error is fine. The one behind it is down, unreachable, or crashed while answering.
If you're a visitor, reload in a minute: many 502s are brief. If it's your site, this guide shows how to work out which server is failing and why.
Why there are two servers involved
Most sites don't serve pages straight from the app. A proxy sits in front and passes each request to the app behind it, often called the upstream or origin:
- Nginx or Apache in front of a Node, Python, PHP-FPM or .NET app on the same server
- Cloudflare or another CDN in front of your web server
- A load balancer in front of several app servers
- A hosting platform's router in front of your container
A 502 comes from the proxy, reporting that the upstream refused the connection, closed it early, or sent a response the proxy couldn't understand.
Step 1: Work out which layer is failing
The error page tells you where the 502 came from.
- "502 Bad Gateway" with "nginx" or "Apache" underneath. Your web server is running, but the app behind it isn't answering. Go to Step 2.
- A Cloudflare-branded error page. It shows a diagram of the browser, Cloudflare and your host. If your host is marked with an error, Cloudflare couldn't get a good answer from your server: check your web server and app the same way. If it shows 520, 521, 522 or 524 instead of 502, see Cloudflare errors 520, 521, 522 and 524.
- Your hosting platform's own error page. The platform couldn't reach your app. Check its logs and deploy status in the dashboard.
Step 2: Check whether the app is running
The most common cause by far: the app behind the proxy has stopped. It crashed, failed to start after a deploy, or was killed for using too much memory.
On a Linux server, check the service and its recent logs. Replace myapp with your service name, such as php8.3-fpm:
sudo systemctl status myapp
sudo journalctl -u myapp -n 50If it isn't running, the log usually says why: a missing environment variable, a bad config file, a port already in use. Fix that, then start it again with sudo systemctl restart myapp.
If it keeps dying with no error, check whether the system killed it for running out of memory:
sudo dmesg -T | grep -i "killed process"A match means the server ran out of memory. Reduce how many workers the app runs, fix the leak, or move to a server with more memory.
Step 3: Read the proxy's error log
The proxy writes down exactly why it gave up. For Nginx, look at /var/log/nginx/error.log:
sudo tail -n 30 /var/log/nginx/error.log| Log message | What it means |
|---|---|
connect() failed (111: Connection refused) | Nothing is listening where Nginx is sending requests. The app is down or on a different port. |
connect() to unix:/run/php/...sock failed (2: No such file or directory) | The socket path in the config doesn't exist. PHP-FPM is stopped or uses a different socket name. |
connect() ... failed (13: Permission denied) | Nginx isn't allowed to open the socket. Check the socket's owner and permissions. |
upstream prematurely closed connection | The app accepted the request, then crashed or hung up before answering. |
upstream sent too big header | The app's response headers, often large cookies, exceed Nginx's buffer. Raise proxy_buffer_size, or fastcgi_buffer_size for PHP. |
On Apache, the equivalent messages appear in its error log, usually under /var/log/apache2/ or /var/log/httpd/.
Step 4: Check the address and port
If the app is running but Nginx still can't connect, they disagree about where to meet. Compare the proxy_pass (or fastcgi_pass) line in your Nginx config with what the app actually listens on:
sudo ss -tlnpWatch for an app listening on 127.0.0.1:3000 while Nginx sends to port 8080, or an app in a Docker container that only listens on its own localhost, which nothing outside the container can reach. Containers need to listen on 0.0.0.0.
Other common causes
- A firewall blocks the proxy. If Cloudflare shows a 502 but the site loads when you visit your server's IP directly, your firewall or a security plugin may be blocking Cloudflare's addresses. Allow Cloudflare's published IP ranges.
- The app is overloaded. 502s that appear only under traffic usually mean every worker is busy and new connections are refused. Add workers if memory allows, or find the slow requests tying them up.
- A deploy is restarting the app. Brief 502s during every deploy mean the proxy sends traffic while the app is still starting. Most platforms can wait for a health check before switching traffic over.
- A slow request timed out. Timeouts usually show as 504 Gateway Timeout instead, but some proxies report them as 502. If the error appears only on slow pages, look at what those pages do.
After the fix
Reload the site in a private window, then watch the proxy's error log for a few minutes to make sure the errors have stopped. If the app crashed, find out why it crashed, so the next 502 doesn't arrive at 2 a.m. To hear about the next outage before your visitors do, set up an uptime monitor.