Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

What a 502 Error Means

With a 502, Nginx itself is running: it is the gateway. The program behind it failed to answer. That separates it from a 504, where the backend answered too slowly, and from a 503, where a service is overloaded or switched off. The codes are compared in HTTP status codes explained.

If You See It on Someone Else's Site

  • Reload after a minute: a 502 during a deployment or restart often clears quickly.
  • Tell the site owner: the fault is on the server, so your browser and network are not the cause.

Read the Error Log

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

The path is the same on Debian, Ubuntu, AlmaLinux, and Rocky Linux; a site with its own error_log line writes there instead. Load the failing page while you watch the log with tail -f, as shown in the tail command. The messages fall into a few groups:

  • connect() failed with "Connection refused" while connecting to upstream: nothing listens on that port; the backend is stopped or uses another port.
  • connect() to a unix socket failed with "No such file or directory": the socket path is wrong, or PHP-FPM is not running.
  • The same with "Permission denied": Nginx may not open the socket, or SELinux blocked the connection.
  • upstream prematurely closed connection: the backend crashed or exited during the request.
  • no live upstreams: every server in an upstream block is marked as failed.

Is the Backend Running?

systemctl status php8.3-fpm
sudo ss -tlnp
ls -l /run/php/

On Debian and Ubuntu the service name carries the PHP version: 8.3 on Ubuntu 24.04, 8.4 on Debian 13, 8.5 on Ubuntu 26.04. On AlmaLinux and Rocky Linux it is php-fpm. For a Node.js or Python app, check its own service and confirm that ss lists its port. If a service fails to start, its log says why: /var/log/php8.3-fpm.log on Debian and Ubuntu, /var/log/php-fpm/ on the Red Hat family, and journalctl -u with the service name for any service; see journalctl.

The PHP-FPM Socket Path

grep -R fastcgi_pass /etc/nginx/
grep '^listen' /etc/php/8.3/fpm/pool.d/www.conf

Both must name the same socket, by default /run/php/php8.3-fpm.sock on Debian and Ubuntu. After a PHP upgrade the socket name changes with the version, while an old server block still points to the previous one. On AlmaLinux and Rocky Linux the pool file is /etc/php-fpm.d/www.conf and the socket /run/php-fpm/www.sock. Fix the path, then run sudo nginx -t and sudo systemctl reload nginx; reload rather than restart, so a mistake leaves the running server untouched.

Socket Permissions

The Nginx user has to be allowed to open the socket. On Debian and Ubuntu the pool file sets listen.owner and listen.group to www-data, and the socket mode is 0660 by default, which matches Nginx. On the Red Hat family, listen.acl_users lists apache and nginx, and the owner and group lines are then ignored. If you changed the user of either side, adjust these lines, test with sudo php-fpm8.3 -t (sudo php-fpm -t on the Red Hat family), and restart PHP-FPM.

A Backend That Crashes

If the service runs but dies under load, look for the kernel's out-of-memory killer with sudo journalctl -k | grep -i oom; see OOM killer in Linux. Read the application's own log for an exception at the time of the 502. For PHP, a fatal error usually gives a 500, but a worker that crashes outright gives a 502.

SELinux on AlmaLinux and Rocky Linux

sudo ausearch -m avc -ts recent
sudo setsebool -P httpd_can_network_connect 1

By default SELinux does not let Nginx connect to arbitrary network ports. If the audit log shows a denial for nginx, the boolean above allows proxying to a port such as 3000. Do not disable SELinux to make the error go away. Proxy setup is in Nginx as a reverse proxy.

Confirm the Fix

curl -I https://example.com/

The first line should show 200. Keep watching the error log for a few minutes of normal traffic. If requests now fail only after a long wait, see 504 Gateway Timeout in Nginx.

Was this answer helpful?
Loading...