Skip to content
JS BRAND LAB

VPS · Problems

nginx 502 Bad Gateway: causes and how to fix it

A 502 Bad Gateway means nginx is alive but cannot get a valid response from the application behind it (Node, Django, PHP-FPM…). Almost always the application is down, ran out of memory, listens on another port or has no permission on the socket. You fix it by looking in three places, in this order: the service, nginx's log and the missing resource (memory or disk).

Updated 2026-11-10 · By Rómulo Felizola · Leer en español

Step 1: is the application alive?

systemctl status myapp
journalctl -u myapp -n 50 --no-pager
ss -ltnp | grep 3020
curl -I http://127.0.0.1:3020

Change myapp and 3020 to your service and port. If the curl straight to the application also fails, the problem is not nginx: it is the application. If it answers fine, the fault is between nginx and it (wrong port, socket, permissions).

Step 2: read what nginx says

tail -n 30 /var/log/nginx/error.log
nginx -t

The key phrases: “connect() failed (111: Connection refused)” (nothing listens on that port, the application is down), “No such file or directory” (the socket path does not exist) and “Permission denied” (nginx cannot open the socket).

The causes we see most

  • The application crashed and nobody restarted it. Fix: run it as a systemd service with Restart=always.
  • It ran out of memory. Check dmesg | grep -i 'out of memory'. If it happens often, the fix is more RAM or a per-service limit so a single application cannot take down the others.
  • A half-finished deployment: files were copied but the service was not restarted, or the new code does not start. journalctl says.
  • A different port or socket than nginx expects after a configuration change.
  • Full disk: the application cannot write and crashes. See VPS out of disk space.

How we have it configured

Each application on our server is a systemd service that restarts itself, with a memory cap, so one application that runs away does not take the others with it:

[Service]
User=myapp
WorkingDirectory=/srv/myapp
Environment="NODE_ENV=production"
Environment="PORT=3020"
Environment="HOSTNAME=127.0.0.1"
ExecStart=/usr/bin/node server.js
Restart=always
RestartSec=5
MemoryMax=400M
NoNewPrivileges=true

The application listens only on 127.0.0.1 and nginx proxies to it. With Restart=always, if the process dies it comes back after 5 seconds and the 502 lasts as long as it takes to start. The full setup is in how to deploy a website on a VPS with nginx.

Frequently asked questions

Are 502 and 504 the same?

No. A 502 is an invalid response or no connection to the application; a 504 (Gateway Timeout) means the application takes too long to answer. You diagnose them the same way, but a 504 points more to a slow or saturated application than to a crash.

Does restarting nginx fix it?

Almost never: nginx is usually fine. Restart or fix the application. Before touching nginx, run nginx -t to check its configuration.

Could it be the provider's problem?

It is rare. In our case, the 17-hour outage in October was our own full disk, not the provider. Rule out your own server first.