Skip to main content
A 502 Bad Gateway in Nginx means Nginx is running but cannot get a valid answer from the program behind it: PHP-FPM, Node.js, Gunicorn or another app. Run sudo tail -n 20 /var/log/nginx/error.log to see the exact reason. Usually the backend is stopped, the socket or port in fastcgi_pass / proxy_pass is wrong, Nginx lacks permission, or the app crashed while answering.

Before you start

  • Connect with SSH. Use sudo, or leave it out if you are root.
  • Reload the page once more: a 502 during a restart of PHP-FPM or your app can be brief and harmless.

Step 1: Read the Nginx error log

If your site has its own error_log line, read that file instead. Find the newest line that contains while connecting to upstream or from upstream, then match it in the table. A 504 Gateway Timeout with upstream timed out (110: Connection timed out) is a different problem: the backend is alive but too slow. Raise proxy_read_timeout or fastcgi_read_timeout and find the slow request.

Step 2: Start the backend

Use your version, for example php8.4-fpm on Debian 13.
If the service fails to start, read why with sudo journalctl -u php8.3-fpm -n 50 (or your service name). For PHP-FPM, sudo php-fpm8.3 -t tests its config.

Step 3: Match the socket or port

Nginx must point to the exact socket or port the backend uses.
A common cause after an OS upgrade or a new PHP version: the config still says php8.2-fpm.sock while only php8.3-fpm.sock exists. Fix the path, then:
Also check the app listens on the same address Nginx connects to. If the app listens on 127.0.0.1:3000, write exactly proxy_pass http://127.0.0.1:3000;.

Step 4: Fix permissions and SELinux

Socket permissions. Nginx runs as www-data (Ubuntu / Debian) or nginx (AlmaLinux / Rocky) and needs read and write access to the socket:
Expected on Ubuntu: srw-rw---- 1 www-data www-data. If you changed listen.owner, listen.group or listen.mode in the pool file (/etc/php/8.3/fpm/pool.d/www.conf), set them back so the Nginx user is the owner or in the group, then restart PHP-FPM. For a Gunicorn socket, see the Group= and --umask settings in deploy with Gunicorn. SELinux (AlmaLinux / Rocky). Allow Nginx to connect to network ports:
Do not disable SELinux to fix a 502. Set the boolean above, and see SELinux on AlmaLinux and Rocky for other denials.

Step 5: Raise buffers for large headers

For upstream sent too big header, add to the server or location block:
Then sudo nginx -t && sudo systemctl reload nginx.

How to check it is fixed

You should get HTTP/1.1 200 OK (or a 301 redirect) instead of 502 Bad Gateway, and no new lines in the error log.

Frequently asked questions

The 502 comes and goes under load. Why?

The backend runs out of workers or memory. Check the PHP-FPM log for server reached pm.max_children setting and raise pm.max_children if you have free RAM. Check for killed processes with out-of-memory errors.

How do I know the 502 comes from my own server?

If your Nginx error log gets a new upstream line each time you load the page, your own Nginx produced the 502 and the fix is on your server. Work through the steps above.

A CDN such as Cloudflare shows a 502. Is that the same?

Not always. If the error page is the CDN’s own page, the CDN could not get a valid reply from your server. Test your server directly with curl as above; if that works, the problem is between the CDN and your server.

VPS and dedicated server guides

Every server guide in one place.

Website not loading

Broader site checks.

Install a LEMP stack

A working PHP-FPM setup.

Nginx reverse proxy

Correct proxy settings.

Read logs with journalctl

Find why a service failed.

Checked against the official nginx proxy module and Red Hat Enterprise Linux 9 Nginx guide on 4 November 2026; the “No such file or directory”, “Connection refused”, “Permission denied” and “prematurely closed” errors reproduced and fixed on Ubuntu 24.04 LTS (Nginx 1.24.0, PHP-FPM 8.3). Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026