502 Bad Gateway Error
Learn what 502 Bad Gateway means, common causes like proxy or load balancer misconfiguration, and how to fix gateway errors.
What Does This Error Mean?
The 502 Bad Gateway status code means the server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed to fulfill the request.
Common Causes
Upstream server (application server) crashed or is unresponsive
Nginx/Apache reverse proxy configuration error
Load balancer timeout waiting for a backend response
Firewall blocking communication between proxy and upstream
SSL/TLS handshake failure between proxy and upstream
Upstream server returns an empty or malformed response
How to Fix It
Check upstream server health
Verify the upstream server (your app server) is running and responding correctly.
# Check if the upstream server is running curl -I http://localhost:3000/health # Should return 200 OK # Check application process systemctl status myapp # or pm2 status
Fix Nginx proxy configuration
Ensure the proxy_pass directive points to the correct upstream address and port.
server {
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
# Increase timeout if needed
proxy_read_timeout 60s;
}
}Check firewall and network
Ensure the proxy server can reach the upstream server through any firewalls or security groups.
# Test connectivity from proxy to upstream telnet upstream-server 3000 # or nc -zv upstream-server 3000
Related Tools
Use these tools to debug and fix this error:
Related Errors
Other common errors in this category:
401 Unauthorized Error
Learn what a 401 Unauthorized error means, common causes, and how to fix authentication failures in your web applications.
403 Forbidden Error
Learn what 403 Forbidden means, how it differs from 401, and how to fix access denied errors in your applications.
404 Not Found Error
Learn what 404 Not Found means, common causes, and how to fix broken links and missing resources on your website or API.
429 Too Many Requests Error
Learn what 429 Too Many Requests means, how rate limiting works, and how to handle or avoid hitting API rate limits.
Frequently Asked Questions
What is the difference between 502 and 504?
502 Bad Gateway means the upstream gave an invalid response. 504 Gateway Timeout means the upstream did not respond within the timeout period.
Can Cloudflare cause a 502 error?
Yes. Cloudflare can return 502 if the origin server is unreachable, the SSL handshake fails, or the origin returns an invalid response. Check Cloudflare's analytics for error details.