502 Bad Gateway Error: What It Means and How to Fix It

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.

How a 502 Error Happens

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.

CauseWhere it originatesFirst check
Upstream app server crashedPHP-FPM, Node.js, GunicornCheck process status
Upstream timeout exceededNginx proxy_read_timeoutIncrease timeout or fix slow app
App server not listening on expected port/socketApp misconfigurationVerify socket/port binding
CDN cannot reach origin serverCDN / load balancerCheck origin server firewall
Memory exhaustion on app serverServer resourcesCheck RAM and OOM killer logs
Deployment broke the appRecent code changeRoll back and check error logs

Quick Diagnosis

Start with these commands to identify which layer is failing:

Terminal — check running services
# 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.log

Fix for Nginx Upstream Errors

Nginx logs the specific upstream error. Check the error log first:

Terminal
sudo grep "upstream" /var/log/nginx/error.log | tail -20

Common 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 upstream

All servers in an upstream block are down. Check each backend server.

Fix for PHP-FPM Crashes

Terminal
# 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)
If PHP-FPM crashes repeatedly, check for memory limits. Increase 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.

Fix for Node.js / App Server Crashes

Terminal — PM2
# 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
Terminal — systemd service
# Check service status
sudo systemctl status myapp

# View recent logs
sudo journalctl -u myapp -n 50 --no-pager

# Restart
sudo systemctl restart myapp

Fix for CDN and Proxy 502s

If you use a CDN (Cloudflare, Fastly, AWS CloudFront), the 502 may be generated by the CDN when it cannot reach your origin server. Check:

  • Your origin server's firewall allows inbound connections from the CDN's IP ranges
  • Your origin server is responding on the correct port (80 or 443)
  • The CDN origin URL is correctly configured (no typos in hostname or path)
  • Your origin server's SSL certificate is valid if the CDN connects via HTTPS
Cloudflare shows a distinctive 502 error page with a Ray ID. Note the Ray ID and check your origin server logs for requests matching that timestamp to identify the exact failure.

Timeout Mismatches

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:

Nginx — proxy timeout settings
# 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;
PHP-FPM — request timeout
# In /etc/php/8.2/fpm/pool.d/www.conf:
request_terminate_timeout = 120

Get Alerted the Moment a 502 Hits

Alive24x7 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

Frequently Asked Questions

Related Articles