DB / Nginx/Apache/Load Balancers / 502
CRITICAL
Nginx/Apache/Load Balancers Bad Gateway
The server, acting as a gateway or proxy, received an invalid response from an upstream server.
Common Causes
- Backend application server (like PHP-FPM, Node.js, or Gunicorn) is down or crashed
- Timeout between proxy server and backend server
- Misconfigured proxy_pass directive in Nginx/Apache
- Firewall or network issues blocking communication to upstream server
- Insufficient resources (CPU/memory) on backend server
How to Fix
1 Check Backend Service Status
Verify if your application server (PHP-FPM, Node, etc.) is running and listening on the correct port.
BASH
$ sudo systemctl status php-fpm
# Or check specific port
sudo netstat -tlnp | grep :9000 2 Test Upstream Connection
Manually test if the proxy can reach the backend server using curl or telnet.
BASH
$ curl -v http://backend-server:3000/
telnet backend-server 3000 3 Increase Proxy Timeouts
Adjust Nginx proxy timeout settings if backend responses are slow.
BASH
$ location / {
proxy_pass http://backend;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
} 4 Check Error Logs
Examine both proxy and backend server logs for detailed error messages.
BASH
$ sudo tail -f /var/log/nginx/error.log
sudo journalctl -u php-fpm -f 5 Verify Firewall Rules
Ensure firewall allows traffic between proxy and backend servers.
BASH
$ sudo iptables -L -n | grep 3000
# If using AWS/GCP, check security group rules