A 502 Bad Gateway error means a gateway server received an invalid or no response from an upstream server. This guide explains every cause and provides targeted fixes for Nginx, Apache, PHP-FPM, Node.js, and CDN setups.
Modern web applications typically have multiple layers: a reverse proxy (Nginx or Apache), an application server (PHP-FPM, Node.js, Python/Gunicorn), and sometimes a CDN in front of everything. A 502 error occurs when one layer cannot get a valid response from the layer behind it.
| Cause | Where it originates | First check |
|---|---|---|
| Upstream app server crashed | PHP-FPM, Node.js, Gunicorn | Check process status |
| Upstream timeout exceeded | Nginx proxy_read_timeout | Increase timeout or fix slow app |
| App server not listening on expected port/socket | App misconfiguration | Verify socket/port binding |
| CDN cannot reach origin server | CDN / load balancer | Check origin server firewall |
| Memory exhaustion on app server | Server resources | Check RAM and OOM killer logs |
| Deployment broke the app | Recent code change | Roll back and check error logs |
Start with these commands to identify which layer is failing:
# Check Nginx
sudo systemctl status nginx
# Check PHP-FPM (adjust version)
sudo systemctl status php8.2-fpm
# Check Node.js app (if using PM2)
pm2 status
# Check recent error logs
sudo tail -50 /var/log/nginx/error.log
sudo tail -50 /var/log/php8.2-fpm.logNginx logs the specific upstream error. Check the error log first:
sudo grep "upstream" /var/log/nginx/error.log | tail -20Common Nginx error messages and their fixes:
connect() failed (111: Connection refused)The upstream service is not running or not listening on the configured port/socket. Restart the upstream service.
upstream timed out (110: Connection timed out)The upstream service is running but too slow. Increase proxy_read_timeout or fix the slow application code.
no live upstreams while connecting to upstreamAll servers in an upstream block are down. Check each backend server.
# Check PHP-FPM status
sudo systemctl status php8.2-fpm
# Restart PHP-FPM
sudo systemctl restart php8.2-fpm
# Check PHP-FPM error log
sudo tail -50 /var/log/php8.2-fpm.log
# Check PHP-FPM pool configuration
sudo nano /etc/php/8.2/fpm/pool.d/www.conf
# Verify: listen = /run/php/php8.2-fpm.sock (must match Nginx fastcgi_pass)pm.max_children in the pool config if the server has available RAM, or reduce memory_limit in php.ini to prevent individual processes from consuming too much memory.# Check PM2 process status
pm2 status
# View logs for crashed process
pm2 logs app-name --lines 50
# Restart the app
pm2 restart app-name
# Enable auto-restart on crash (if not already set)
pm2 startup
pm2 save# Check service status
sudo systemctl status myapp
# View recent logs
sudo journalctl -u myapp -n 50 --no-pager
# Restart
sudo systemctl restart myappIf you use a CDN (Cloudflare, Fastly, AWS CloudFront), the 502 may be generated by the CDN when it cannot reach your origin server. Check:
A common cause of intermittent 502 errors is a timeout mismatch between layers. If Nginx times out waiting for PHP-FPM before PHP-FPM finishes processing, Nginx returns a 502. The fix is to ensure timeouts increase from front to back:
# In your server or location block:
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 120s; # Must be > your app's processing time
# For PHP-FPM via FastCGI:
fastcgi_read_timeout 120s;# In /etc/php/8.2/fpm/pool.d/www.conf:
request_terminate_timeout = 120Alive24x7 checks your website every minute and sends instant alerts when a 502 Bad Gateway error is detected — via email, SMS, or Slack. Catch outages before your customers do.
Start Free Monitoring