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 areroot. - 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
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
- PHP-FPM (Ubuntu / Debian)
- PHP-FPM (AlmaLinux / Rocky)
- Node.js / Python app
php8.4-fpm on Debian 13.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.php8.2-fpm.sock while only php8.3-fpm.sock exists. Fix the path, then:
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 aswww-data (Ubuntu / Debian) or nginx (AlmaLinux / Rocky) and needs read and write access to the socket:
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:
Step 5: Raise buffers for large headers
Forupstream sent too big header, add to the server or location block:
sudo nginx -t && sudo systemctl reload nginx.
How to check it is fixed
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 forserver 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 newupstream 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 withcurl as above; if that works, the problem is between the CDN and your server.
Related guides
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:- Live Chat: Quick assistance via our website.
- Support Ticket: Open a Ticket
- Discord: Join our Community
- Email: support@freakhosting.com
Last Updated: November 2026