DB / Nginx / 502
CRITICAL

Nginx Bad Gateway

Nginx received an invalid response from an upstream server (like PHP-FPM, Node.js, or another backend) while acting as a reverse proxy or load balancer.

Common Causes

  • Upstream server (application backend) is down or not responding
  • Upstream server process crashed or is stuck
  • Network connectivity issues between Nginx and upstream server
  • Upstream server timeout settings are too low
  • Resource exhaustion (memory, CPU) on the upstream server

How to Fix

1 Check Upstream Server Status

Verify if your application backend (PHP-FPM, Gunicorn, etc.) is running and accessible.

BASH
$ systemctl status php8.1-fpm # or your specific service curl -I http://localhost:3000 # test backend directly

2 Increase Proxy Timeouts

Adjust Nginx proxy timeout directives if upstream is slow to respond.

BASH
$ location / { proxy_pass http://backend; proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; }

3 Examine Nginx Error Logs

Check Nginx error logs for specific upstream failure messages.

BASH
$ tail -f /var/log/nginx/error.log grep "502" /var/log/nginx/error.log

4 Test Upstream Connectivity

Verify network connectivity and port accessibility to the upstream server.

BASH
$ telnet backend-server-ip 9000 # test PHP-FPM port nc -zv localhost 3000 # test Node.js backend

5 Restart Upstream Services

Restart the application backend service and Nginx.

BASH
$ systemctl restart php8.1-fpm systemctl restart nginx