502
Server Error
502 Bad Gateway: When One Server Gets a Bad Response from Another
✦ The Golden Answer: The 502 Bad Gateway status code means that a server, acting as a gateway or proxy, received an invalid response from an upstream server it accessed while attempting to fulfill the request. In simpler terms: your server talked to another server, but the other server sent back nonsense or didn't respond correctly. As a visitor, it's usually temporary — refresh or wait. As a site owner, the fix involves checking server logs, ensuring your backend services (like PHP-FPM, Node.js, or Apache) are running, and verifying that your firewall/CDN isn't blocking legitimate traffic.
Common Causes of 502 Bad Gateway
| Cause | Example | How to Fix |
|---|---|---|
| Backend service is down | PHP-FPM or Node.js process crashed. | Restart the service; check logs for errors. |
| Firewall blocking traffic | Server firewall (iptables, CSF) or WAF blocks requests to the upstream. | Check firewall logs and whitelist necessary IPs. |
| CDN misconfiguration | Cloudflare cannot reach your origin server due to DNS or firewall issues. | Check CDN dashboard; ensure origin IP is correct and accessible. |
| DNS propagation issues | Recent DNS change hasn't fully propagated, causing some users to hit outdated servers. | Wait for propagation (1-48 hours) or flush DNS. |
| Server overload | Upstream server is overloaded and cannot respond in time. | Scale up resources or optimize the application. |
| SSL/TLS handshake failure | Certificate mismatch or expired certificate between gateway and upstream. | Renew certificates, check certificate chains. |
Example: Nginx Error Log
When Nginx acts as a reverse proxy and fails to connect to the upstream, you'll see something like:
2026/06/28 10:15:23 [error] 1234#1234: *34 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.1, server: example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "example.com"
This indicates Nginx tried to connect to port 8080 but the service was not running. Restart the backend service.
How to Fix 502 Errors
- For visitors: Refresh the page (F5), wait a few minutes, clear browser cache, or restart your router. The issue is often temporary.
- For site owners: Check the server logs (e.g., /var/log/nginx/error.log, /var/log/apache2/error.log). Verify that backend services are running (
sudo systemctl status php-fpm). Check firewall rules and CDN settings. Test connectivity to the upstream server.
Preventing 502 Errors
- Implement health checks – use monitoring to detect upstream failures early.
- Use load balancing – distribute traffic across multiple upstream servers.
- Set proper timeouts – avoid premature timeouts on slow backends.
- Enable retries – configure proxies to retry failed requests to a backup upstream.
Frequently Asked Questions
Is a 502 Bad Gateway my fault?
As a visitor, no — the problem is almost always on the website's server. As a site owner, it's often a configuration or resource issue that you can fix.
What's the difference between 502 and 504?
502 Bad Gateway means the upstream server sent back an invalid response. 504 Gateway Timeout means the upstream server didn't respond at all before the gateway timed out.
Can a CDN cause a 502 error?
Yes. If your CDN (like Cloudflare) can't connect to your origin server, it will often display a 502 error to visitors. Check your CDN's dashboard for origin server status.