# Incident note: HTTP 502 Bad Gateway (nginx, PHP-FPM): how to fix

> Incident note: 502 Bad Gateway in nginx: the backend (PHP-FPM, Node, Gunicorn) is not answering. Common causes, what to look for in error.log and quick…

Author: Daniele Michelin · Published: 2026-11-30 · Updated: 2026-11-30 · Language: en
Canonical: https://daniele-michelin.com/en/blog/incident-note-http-502/

nginx could not get a valid response from the upstream (PHP-FPM, Node.js, Gunicorn, a Docker container) – the backend is down, unreachable or crashed.

## Read the signal

Start with the exact wording of the error and its timestamp in the log. Then narrow down the cause before changing anything.

```
2026/10/23 09:14:02 [error] 1234#1234: *5678 connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 198.51.100.7, server: cloud.beispiel.de, request: "GET / HTTP/2.0", upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock:"
```

## Narrow down possible causes

- PHP-FPM is stopped or crashed (e.g. after an update or an OOM kill).
- The socket path no longer matches after a PHP upgrade, e.g. `php8.2-fpm.sock` instead of `php8.3-fpm.sock`.
- No permission on the socket: `listen.owner`/`listen.group` do not match the nginx user.
- The backend listens on a different port or only on IPv6/IPv4.

## Check before changing anything

- Read the latest errors in the nginx log

```
tail -n 50 /var/log/nginx/error.log
```

- Check PHP-FPM status

```
systemctl status php8.3-fpm
```

- Does the socket exist and who owns it?

```
ls -l /run/php/
```

- PHP-FPM logs, including crashes

```
journalctl -u php8.3-fpm -n 50 --no-pager
```

## Fix and test again

- Restart PHP-FPM or the backend with `systemctl restart php8.3-fpm`, then look for the root cause in the journal.
- Point `fastcgi_pass` to the correct socket, then `nginx -t && systemctl reload nginx`.
- Set `listen.owner = www-data` and `listen.group = www-data` in the FPM pool.
- On OOM kills, size `pm.max_children` to the available RAM.
Then repeat the original action and compare the result with the initial error.

## Keep a note for the next ticket

Record the exact error, the check result, the change and the retest. That gives the team a useful starting point next time.

## What is the difference between 502 and 504?
502: the backend does not answer at all or answers garbage. 504: the backend is reachable but takes longer than the nginx timeout.

[Full error guide: HTTP 502 Bad Gateway with nginx and PHP-FPM](https://daniele-michelin.com/en/errors/http-502-bad-gateway/)
