502 Bad Gateway Error: What It Means, Common Causes and How to Fix It
Quick answer: A 502 Bad Gateway happens when a server in the middle, such as Nginx, a reverse proxy, a load balancer or Cloudflare, gets an invalid response (or none at all) from the application server behind it. In practice, PHP-FPM, the app or the backend API crashed, froze or isn't accepting connections. Check that those services are running, restart them, and read the proxy's error log to see exactly why the connection failed.
The 502 Bad Gateway error is one of the most common server errors on modern websites, because most sites now have at least two layers: a web server or proxy in front, and an application behind it. When the front layer can't get a valid answer from the back, the visitor sees a 502.
This guide explains what the error means, where it comes from in the most common setups and how to fix it step by step.
What does 502 Bad Gateway mean?
The 502 status code means that a server acting as a gateway or proxy received an invalid response from the upstream server it forwarded the request to. The request reached your infrastructure, but the chain broke between two of its parts:
Browser -> Cloudflare / load balancer -> Nginx -> PHP-FPM / Node / API
(any of these links can fail)
The visitor's browser, internet connection and DNS are working: the problem is on the server side. If you are a visitor and not the site owner, all you can do is reload after a minute or two; if it persists, the site has to fix it.
Common causes of a 502
- The application process is down: PHP-FPM, Node, Python or Java stopped or crashed;
- No free workers: all PHP-FPM children are busy, so new requests are refused;
- The app crashed while answering: a fatal error, out-of-memory kill or segfault cut the response;
- Wrong proxy configuration: the proxy points to the wrong socket, port or host;
- Firewall or security group: blocks the connection between proxy and backend;
- Response headers too large: big cookies or headers exceed the proxy's buffer;
- Server overload: CPU or memory exhausted;
- Origin unreachable from the CDN: the origin IP changed, the server is down, or it blocks the CDN's IPs.
How to fix a 502 Bad Gateway, step by step
1. Check whether the backend services are running
On a Linux server with Nginx and PHP-FPM:
sudo systemctl status nginx
sudo systemctl status php-fpm # or php8.2-fpm on Debian/Ubuntu
sudo systemctl restart php-fpm
For a Node app managed by PM2, use pm2 status and pm2 logs; for Docker, docker ps and docker logs. Restarting brings the site back, but if you don't find the cause, the 502 will return.
2. Read the proxy's error log
The error log almost always says exactly what failed:
sudo tail -n 50 /var/log/nginx/error.log
| Message in the log | What it usually means |
|---|---|
connect() to unix:/run/php-fpm/www.sock failed (2: No such file or directory) | PHP-FPM isn't running, or Nginx points to the wrong socket |
connect() ... failed (13: Permission denied) | Nginx can't access the PHP-FPM socket (check listen.owner / listen.group) |
connect() failed (111: Connection refused) while connecting to upstream | Nothing is listening on that port: the app is down or on another port |
upstream prematurely closed connection | The app crashed or was killed while answering |
upstream sent too big header | Response headers exceed the buffer (large cookies) |
On Apache with a proxy, look at /var/log/apache2/error.log (Debian/Ubuntu) or /var/log/httpd/error_log (RHEL/Amazon Linux).
3. Check the application's own logs
If the proxy says the upstream closed the connection, look at the PHP-FPM log and the PHP error log for fatal errors and memory problems:
sudo tail -n 50 /var/log/php-fpm/error.log
sudo dmesg -T | grep -i -E "killed process|out of memory"
A message like server reached pm.max_children setting means all workers were busy. Raise pm.max_children only if the server has free memory, and look for slow pages that hold workers for too long. See how to find the cause of a PHP 500 error in the logs.
4. Verify the proxy configuration
The socket or port in the proxy must match where the app is listening:
# Nginx
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock; # must match "listen" in the PHP-FPM pool
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
After any change, test the configuration before reloading: sudo nginx -t (or sudo apachectl configtest on Apache), then sudo systemctl reload nginx.
5. Fix "too big header" errors
# Nginx, inside the server or location block
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 16k; # when using proxy_pass
proxy_buffers 8 16k;
6. Check server resources
Run top or htop and free -h. If memory is exhausted, the kernel kills processes and the proxy answers 502. Reduce memory use, add swap or upgrade the server.
7. If you use Cloudflare or a load balancer
- If the 502 page shows Cloudflare branding, Cloudflare couldn't get a valid answer from your origin: check that the server is up and that the DNS record points to the right IP;
- Make sure the origin firewall allows Cloudflare's IP ranges;
- Temporarily switch the record to "DNS only" to test the origin directly;
- On AWS, an Application Load Balancer returns 502 when targets close the connection or return malformed responses: check the target group health checks and the app logs.
502 in APIs and web applications
In APIs, a 502 usually means the backend service didn't answer the gateway correctly. Check the service status, its response time, and whether a recent deploy broke it. If a request depends on a third-party API, the 502 may come from that API: handle failures with timeouts and clear error messages instead of passing the 502 through.
502 vs. 500, 503 and 504
| Code | Meaning | Typical cause |
|---|---|---|
| 500 | Internal Server Error | The application itself failed (code or configuration error) |
| 502 | Bad Gateway | The proxy got an invalid response or no connection from the backend |
| 503 | Service Unavailable | The server is overloaded or in maintenance, on purpose or not |
| 504 | Gateway Timeout | The backend answered too slowly |
Each code points to a different kind of failure. If yours is a timeout, see 504 Gateway Timeout.
How to prevent 502 errors
- Run the app under a process manager that restarts it automatically (systemd, PM2, Docker restart policies);
- Size PHP-FPM workers to the available memory;
- Monitor uptime and alert on 5xx errors;
- Test proxy configuration before every reload;
- Deploy without downtime (reload instead of stop/start).
Conclusion
A 502 Bad Gateway means the communication between two of your servers failed. Restarting the backend usually brings the site back, but the proxy's error log tells you why it happened: a stopped service, a wrong socket, a crash or exhausted resources. Fix that cause and the 502 won't come back.
Frequently asked questions about 502 errors
Is a 502 error my fault as a visitor?
No. It is a server-side problem. Reload after a minute; if it continues, the site owner needs to fix it.
Does a 502 error hurt SEO?
Short outages don't. If Googlebot sees 5xx errors for hours or days, it crawls less and can eventually drop pages, so fix recurring 502s quickly.
Why does the 502 come and go?
Usually because workers run out under load, or the app crashes on certain requests. Check the logs at the times the errors happen.
What is the difference between 502 and 504?
502 means the backend gave an invalid answer or refused the connection; 504 means it took too long to answer.
How do I know whether Cloudflare or my server is the problem?
Cloudflare's error page shows which side failed. If it says the host has an error, the problem is your origin server.