DEV Community

Cover image for Incident note: HTTP 502 Bad Gateway (nginx, PHP-FPM): how to fix
Daniele Michelin
Daniele Michelin

Posted on Originally published at daniele-michelin.com

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

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:"
Enter fullscreen mode Exit fullscreen mode

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

1.

Read the latest errors in the nginx log

tail -n 50 /var/log/nginx/error.log
Enter fullscreen mode Exit fullscreen mode

2.

Check PHP-FPM status

systemctl status php8.3-fpm
Enter fullscreen mode Exit fullscreen mode

3.

Does the socket exist and who owns it?

ls -l /run/php/
Enter fullscreen mode Exit fullscreen mode

4.

PHP-FPM logs, including crashes

journalctl -u php8.3-fpm -n 50 --no-pager
Enter fullscreen mode Exit fullscreen mode

Fix and test again

  1. Restart PHP-FPM or the backend with systemctl restart php8.3-fpm, then look for the root cause in the journal.
  2. Point fastcgi_pass to the correct socket, then nginx -t && systemctl reload nginx.
  3. Set listen.owner = www-data and listen.group = www-data in the FPM pool.
  4. 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


Originally published at daniele-michelin.com. More guides, free checks (SPF, TLS, headers) and an error-message lexicon: daniele-michelin.com/en/.

Top comments (0)