How to Fix 502 Bad Gateway Error
The 502 Bad Gateway error means a server acting as a gateway received an invalid response from an upstream server. Here's how to fix it.
What is a 502 Bad Gateway Error?
A 502 Bad Gateway error occurs when one server (acting as a gateway or proxy) receives an invalid response from another server it's trying to reach. This is a server-side error, meaning the problem is with the website's infrastructure, not your computer or connection.
Common scenarios where you'll see 502 errors:
- A reverse proxy (like Nginx) can't reach the application server
- A CDN (like Cloudflare) can't connect to the origin server
- A load balancer receives a bad response from backend servers
- The upstream server is overloaded or crashed
Quick Check
Test if your site is returning 502 errors using our free URL Status Checker. Check multiple pages at once to see the scope of the problem.
Common Causes of 502 Bad Gateway
- Origin Server Down - The backend server crashed or stopped
- Server Overload - Too many requests overwhelming the server
- PHP-FPM Issues - PHP process manager not responding
- Timeout Issues - Upstream server taking too long to respond
- Firewall Blocking - Firewall rules blocking internal communication
- DNS Problems - Incorrect DNS resolution between servers
- Bad Deployment - Recent code deploy broke the application
- Memory Exhaustion - Server ran out of RAM
How to Fix 502 Bad Gateway: For Visitors
If you're just visiting a website and see a 502 error:
1. Refresh the Page
Sometimes 502 errors are temporary. Wait a few seconds and refresh (Ctrl+F5 or Cmd+Shift+R for a hard refresh).
2. Clear Browser Cache
Your browser might have cached a bad response. Clear your cache and cookies, then try again.
3. Try a Different Browser or Device
Rule out browser-specific issues by trying another browser or your phone.
4. Check if the Site is Down for Everyone
Use a service like URL Status Checker to see if the site returns 502 from our servers too.
How to Fix 502 Bad Gateway: For Website Owners
1. Check if Your Origin Server is Running
# Check if web server is running
sudo systemctl status nginx
sudo systemctl status apache2
# Check if application is running
sudo systemctl status php-fpm
pm2 status # For Node.js apps
# Restart if needed
sudo systemctl restart nginx
sudo systemctl restart php-fpm
2. Check Server Logs
# Nginx error log
sudo tail -f /var/log/nginx/error.log
# Apache error log
sudo tail -f /var/log/apache2/error.log
# PHP-FPM log
sudo tail -f /var/log/php-fpm/error.log
# Application logs
sudo tail -f /var/www/myapp/logs/error.log
3. Check Server Resources
# Check CPU and memory usage
htop
free -m
# Check disk space
df -h
# Check for zombie processes
ps aux | grep -E 'Z|defunct'
Memory Issues
If your server is out of memory, PHP-FPM and other processes will crash, causing 502 errors. Consider upgrading your server or optimizing your application.
4. Check PHP-FPM Configuration
Most 502 errors with PHP applications are caused by PHP-FPM issues:
# Check PHP-FPM status
sudo systemctl status php8.2-fpm
# Edit PHP-FPM pool config
sudo nano /etc/php/8.2/fpm/pool.d/www.conf
# Important settings to check:
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
# Restart after changes
sudo systemctl restart php8.2-fpm
5. Increase Timeout Values
If your application is slow, increase proxy timeouts in Nginx:
# In nginx.conf or site config
location / {
proxy_pass http://127.0.0.1:3000;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# For PHP-FPM
location ~ \.php$ {
fastcgi_read_timeout 300;
fastcgi_send_timeout 300;
}
6. Check Firewall Rules
# Check if internal ports are open
sudo ufw status
# Allow internal communication
sudo ufw allow from 127.0.0.1
# Check iptables
sudo iptables -L -n
502 Error with Cloudflare
If you're using Cloudflare and see a 502 error:
- Error 502 - Cloudflare received a bad response from your origin
- Error 521 - Your origin server is down (Cloudflare-specific)
- Error 522 - Connection timed out to origin
Solutions:
- Check your origin server is running
- Whitelist Cloudflare IP ranges in your firewall
- Temporarily enable "Development Mode" to bypass cache
- Check Cloudflare's system status page
502 vs Other 5xx Errors
| Error | Meaning | Likely Cause |
|---|---|---|
500 |
Internal Server Error | Application code bug |
502 |
Bad Gateway | Upstream server issue |
503 |
Service Unavailable | Server overloaded/maintenance |
504 |
Gateway Timeout | Upstream server too slow |
Preventing 502 Errors
- Monitor your servers - Set up uptime monitoring to catch issues early
- Scale appropriately - Add more server resources before you need them
- Use health checks - Configure load balancers to detect unhealthy backends
- Implement graceful restarts - Don't kill processes during deployments
- Optimize your application - Slow code leads to timeouts and 502s
Verify Your Fix
After making changes, use URL Status Checker to verify your site now returns 200 OK across all pages.