← Back to Error Lab

Nginx: 502 Bad Gateway

Published 2026-04-15

Educational use only. Content explains errors and defensive fixes for systems you own or are authorised to test. Do not use any technique here to access data, accounts, or networks without permission.

Root Cause

A 502 Bad Gateway error indicates that Nginx, acting as a reverse proxy, received an invalid response from the upstream server it was trying to communicate with. In the context of modern web deployments, this almost always means that the backend application server (like Node.js, Python Gunicorn, or PHP-FPM) is either down, crashed, restarting, or rejecting connections. Nginx is working perfectly fine, but the service it is trying to forward the request to is not available at the specified port or socket.

Fix / Solution

The issue lies with the backend service, not Nginx. Check the status of your application server (`systemctl status myapp` or `docker ps`). Inspect the application logs for crashes. Also, verify that the Nginx configuration (`proxy_pass`) points to the correct IP address and port where the backend service is actually listening. If using Unix sockets, ensure the socket file exists and Nginx has permission to read/write to it.

Code Snippet

# ❌ Nginx config pointing to a dead port
location /api/ {
    proxy_pass http://127.0.0.1:3000;
}

# ✅ Check backend service status
sudo systemctl status my-node-app

# ✅ Check application logs for crashes
journalctl -u my-node-app -e