Short answer
A 502 Bad Gateway means Nginx could not get a good answer from the program behind it: PHP-FPM or your app. Run sudo tail -n 1 /var/log/nginx/error.log right after the error. The text in brackets names the cause: 2: No such file or directory, 13: Permission denied, 111: Connection refused, or upstream sent too big header.
Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04? 3 sections differ, each step marked “On Ubuntu 24.04” right under it: Cause 1: PHP-FPM is not running (2: No such file or directory), Cause 3: the socket belongs to the wrong user (13: Permission denied), Cause 6: the PHP worker died during the request (104: Connection reset by peer). Everything that differed
Step 1: read the error log
The 502 page itself says nothing useful. Load the page that fails once more (or use curl), then read the last line of Nginx’s error log at once:
$ curl -sI http://localhost/test.php | head -n 1HTTP/1.1 502 Bad Gateway$ sudo tail -n 1 /var/log/nginx/error.log2026/11/19 16:56:14 [crit] 8773#8773: *5 connect() to unix:/run/php/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: ::1, server: _, request: "HEAD /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost"Look for three things in that line: the message after connect() or recv() with its number in brackets, the upstream: part (where Nginx tried to send the request), and the request that failed. Then find your message in this table:
connect() to unix:… failed (2: No such file or directory): PHP-FPM is stopped, or the socket path is wrong.connect() to unix:… failed (13: Permission denied): the socket belongs to the wrong user.connect() failed (111: Connection refused): the app behind `proxy_pass` is not running.upstream sent too big header: the response headers do not fit the buffer.recv() failed (104: Connection reset by peer): the PHP worker died during the request.
The test server in this guide runs a normal setup: Nginx and PHP-FPM from Ubuntu’s archive, the default site passing .php files to PHP-FPM, and /app/ passed to a small app on 127.0.0.1:8000. This is its site file, /etc/nginx/sites-available/default:
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html; server_name _; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock; } location /app/ { proxy_pass http://127.0.0.1:8000/; }}Each cause below was created on purpose on that server, then found and fixed with the commands shown. The log lines are the ones it printed.
Cause 1: PHP-FPM is not running (2: No such file or directory)
The log line above is this case: Nginx looked for the socket file, and it was not there. PHP-FPM creates the socket when it starts and removes it when it stops. Find the service’s name first, because it carries the PHP version:
$ systemctl list-units --all --no-pager 'php*-fpm.service' UNIT LOAD ACTIVE SUB DESCRIPTION php8.5-fpm.service loaded inactive dead The PHP 8.5 FastCGI Process Manager Legend: LOAD → Reflects whether the unit definition was properly loaded. ACTIVE → The high-level unit activation state, i.e. generalization of SUB. SUB → The low-level unit activation state, values depend on unit type. 1 loaded units listed.To show all installed unit files use 'systemctl list-unit-files'.$ ls -l /run/php/total 0lrwxrwxrwx 1 root root 30 Nov 19 16:56 php-fpm.sock -> /etc/alternatives/php-fpm.sockinactive dead, and /run/php/ holds only the php-fpm.sock link, which points to a socket that does not exist. Start the service, and enable it so it starts at boot too:
$ sudo systemctl enable --now php8.5-fpmSynchronizing state of php8.5-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.Executing: /usr/lib/systemd/systemd-sysv-install enable php8.5-fpm$ curl -s http://localhost/test.phpPHP 8.5.4 OKOn Ubuntu 24.04
Ubuntu 24.04 ships PHP 8.3, so the service is php8.3-fpm. Everything else in this guide is the same: use 8.3 wherever the commands say 8.5.
$ sudo systemctl enable --now php8.3-fpmSynchronizing state of php8.3-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.Executing: /usr/lib/systemd/systemd-sysv-install enable php8.3-fpmIf it does not start, the service’s journal says why; the usual reason is a mistake in a pool file.
Cause 2: fastcgi_pass names a PHP version you do not have
The same error number, but this time PHP-FPM is running. The path in the log names an old version, php7.4: the site file was copied from an older server, or PHP was upgraded.
$ sudo tail -n 1 /var/log/nginx/error.log2026/11/19 16:56:18 [crit] 10089#10089: *9 connect() to unix:/run/php/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: ::1, server: _, request: "HEAD /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.4-fpm.sock:", host: "localhost"$ ls -l /run/php/total 4lrwxrwxrwx 1 root root 30 Nov 19 16:56 php-fpm.sock -> /etc/alternatives/php-fpm.sock-rw-r--r-- 1 root root 4 Nov 19 16:56 php8.5-fpm.pidsrw-rw---- 1 www-data www-data 0 Nov 19 16:56 php8.5-fpm.sockThe real socket is php8.5-fpm.sock. Find every site that still names the old one:
$ grep -Rn 'fastcgi_pass' /etc/nginx/sites-enabled//etc/nginx/sites-enabled/default:14: fastcgi_pass unix:/run/php/php7.4-fpm.sock;Point it to /run/php/php-fpm.sock instead of a versioned name. On Ubuntu that path is a link (through /etc/alternatives) to the socket of the PHP version installed, so the site keeps working after the next PHP upgrade. Then test the config and reload:
$ sudo sed -i 's#unix:/run/php/php7.4-fpm.sock#unix:/run/php/php-fpm.sock#' /etc/nginx/sites-available/default$ sudo nginx -t && sudo systemctl reload nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful$ curl -s http://localhost/test.phpPHP 8.5.4 OKIf grep -r finds nothing in sites-enabled
The files in /etc/nginx/sites-enabled/ are links to sites-available. grep -r skips links it finds inside a folder, so it prints nothing even when the line is there. Use a capital -R, which follows them, as above. We hit this ourselves while testing this guide.
Cause 3: the socket belongs to the wrong user (13: Permission denied)
$ sudo tail -n 1 /var/log/nginx/error.log2026/11/19 16:56:21 [crit] 10578#10578: *13 connect() to unix:/run/php/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: ::1, server: _, request: "HEAD /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost"The socket exists, but Nginx’s workers (user www-data) may not open it. Look at its owner and mode:
$ ls -l /run/php/php8.5-fpm.socksrw-rw---- 1 nobody nogroup 0 Nov 19 16:56 /run/php/php8.5-fpm.socksrw-rw---- means only the owner and the group may use it, and both are nobody/nogroup. The owner comes from the PHP-FPM pool file:
$ grep -n '^listen\.' /etc/php/8.5/fpm/pool.d/www.conf53:listen.owner = nobody54:listen.group = nogroupThis happens when a pool file is copied for another site user and the listen. lines are changed too. The workers may run as another user (user = and group =), but the socket must stay readable for www-data. Put it back, test the PHP-FPM config, and restart:
$ sudo sed -i 's/^listen.owner = .*/listen.owner = www-data/; s/^listen.group = .*/listen.group = www-data/' /etc/php/8.5/fpm/pool.d/www.conf$ sudo php-fpm8.5 -t && sudo systemctl restart php8.5-fpm[19-Nov-2026 16:56:22] NOTICE: configuration file /etc/php/8.5/fpm/php-fpm.conf test is successful$ ls -l /run/php/php8.5-fpm.socksrw-rw---- 1 www-data www-data 0 Nov 19 16:56 /run/php/php8.5-fpm.sock$ curl -s http://localhost/test.phpPHP 8.5.4 OKOn Ubuntu 24.04
The pool file is /etc/php/8.3/fpm/pool.d/www.conf and the test command is php-fpm8.3. The run printed the same lines:
$ grep -n '^listen\.' /etc/php/8.3/fpm/pool.d/www.conf53:listen.owner = nobody54:listen.group = nogroup$ sudo php-fpm8.3 -t && sudo systemctl restart php8.3-fpm[19-Nov-2026 16:58:25] NOTICE: configuration file /etc/php/8.3/fpm/php-fpm.conf test is successfulCause 4: the app behind proxy_pass is not running (111: Connection refused)
For a site that passes requests to an app (Node.js, Python, a Docker container) with proxy_pass, the log line looks different:
$ curl -sI http://localhost/app/ok.txt | head -n 1HTTP/1.1 502 Bad Gateway$ sudo tail -n 1 /var/log/nginx/error.log2026/11/19 16:56:24 [error] 10577#10577: *17 connect() failed (111: Connection refused) while connecting to upstream, client: ::1, server: _, request: "HEAD /app/ok.txt HTTP/1.1", upstream: "http://127.0.0.1:8000/ok.txt", host: "localhost"upstream: gives the address and port Nginx tried. Check whether anything listens there:
$ sudo ss -tlnp 'sport = :8000'State Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessOnly the header line: nothing listens on port 8000. Start the app through its service (here demo-app; use your app’s, or docker start for a container), and check again:
$ sudo systemctl start demo-app$ sudo ss -tlnp 'sport = :8000'State Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0 5 127.0.0.1:8000 0.0.0.0:* users:(("python3",pid=11823,fd=3))$ curl -s http://localhost/app/ok.txtokIf the app runs but listens on another address or port than the one in upstream:, change one of them so they match. Nginx needs no restart for an app that comes back: the next request gets through.
Cause 5: “upstream sent too big header”
PHP-FPM answered, but its response headers did not fit in Nginx’s buffer. Big cookies, long redirect URLs or a debug header cause it. The test page sends one 8 KB header:
$ curl -sI http://localhost/big.php | head -n 1HTTP/1.1 502 Bad Gateway$ sudo tail -n 1 /var/log/nginx/error.log2026/11/19 16:56:26 [error] 10577#10577: *21 upstream sent too big header while reading response header from upstream, client: ::1, server: _, request: "HEAD /big.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost"The buffer is one memory page by default, 4 KB on most servers. Add two lines under fastcgi_pass in the PHP location: a 16 KB buffer for the headers, and 16 buffers of 16 KB for the rest of the response:
$ sudo sed -i 's#fastcgi_pass unix:/run/php/php-fpm.sock;#&\n fastcgi_buffer_size 16k;\n fastcgi_buffers 16 16k;#' /etc/nginx/sites-available/default$ sudo nginx -t && sudo systemctl reload nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful$ curl -s http://localhost/big.phpbig headerFor a site behind proxy_pass, the same fix uses proxy_buffer_size and proxy_buffers. If the headers keep growing, find out why (a cookie that grows on every request) before you raise the buffer again.
Cause 6: the PHP worker died during the request (104: Connection reset by peer)
Nginx reached PHP-FPM, but the worker handling the request died before it answered. The test page kills its own worker:
$ curl -sI http://localhost/crash.php | head -n 1HTTP/1.1 502 Bad Gateway$ sudo tail -n 1 /var/log/nginx/error.log2026/11/19 16:56:28 [error] 12469#12469: *25 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: ::1, server: _, request: "HEAD /crash.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "localhost"Nginx cannot say why. PHP-FPM’s own log can:
$ sudo tail -n 2 /var/log/php8.5-fpm.log[19-Nov-2026 16:56:28] WARNING: [pool www] child 11241 exited on signal 9 (SIGKILL) after 5.743354 seconds from start[19-Nov-2026 16:56:28] NOTICE: [pool www] child 12628 startedexited on signal 9 (SIGKILL) is a worker killed from outside; on a real server that is most often the kernel’s out-of-memory killer, which records it in the kernel log. PHP-FPM starts a new worker by itself, so other pages keep working:
$ curl -s http://localhost/test.phpPHP 8.5.4 OKOn Ubuntu 24.04
$ sudo tail -n 2 /var/log/php8.3-fpm.log[19-Nov-2026 16:58:30] WARNING: [pool www] child 10764 exited on signal 9 (SIGKILL) after 4.895434 seconds from start[19-Nov-2026 16:58:30] NOTICE: [pool www] child 11862 startedTroubleshooting
(2: No such file or directory)and PHP-FPM is running: the socket path infastcgi_passis wrong. Details.grep -rfinds nofastcgi_pass: usegrep -R;sites-enabledholds links. Details.[crit]or[error]? A missing socket or a permission problem is logged as[crit], a refused connection as[error]. Both give the same 502 page.- 502 only sometimes: look for
exited on signalin the PHP-FPM log, and for the out-of-memory killer. Details.
What these steps changed on the server
Both runs recorded the server before the first step and after the last (see the Tested on box below). The fixes leave the site file with the version-free socket and the bigger buffers, and the pool file with www-data as the socket’s owner.
Ubuntu 26.04 vs 24.04
The same check ran on a fresh 26.04 server and on a fresh 24.04 server, each with its own run log. Every cause gave the same 502 and the same log line on both. Only the PHP version differed:
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-43-generic | Ubuntu 24.04.5 LTS6.8.0-147-generic |
| nginx | 1.28.3-2ubuntu1.13 | 1.24.0-2ubuntu7.20 |
| php-fpm | 2:8.5+99ubuntu1 | 2:8.3+93ubuntu2 |
| php8.5-fpm | 8.5.4-0ubuntu1.5 | not installed |
| python3 | 3.14.3-0ubuntu2 | 3.12.3-0ubuntu2.1 |
| iproute2 | 6.19.0-1ubuntu1.1 | 6.1.0-1ubuntu6.4 |
| curl | 8.18.0-1ubuntu2.8 | 8.5.0-2ubuntu10.16 |
| php8.3-fpm | not installed | 8.3.6-0ubuntu0.24.04.13 |
| PHP-FPM service, socket and pool file | php8.5-fpm, /run/php/php8.5-fpm.sock, /etc/php/8.5/fpm/ | php8.3-fpm, /run/php/php8.3-fpm.sock, /etc/php/8.3/fpm/ |
| Proof | Run log, 45 checks passed | Run log, 45 checks passed |
Undo: remove the test pages
If you made test pages to reproduce an error, remove them. The fixes themselves are worth keeping.
$ sudo rm /var/www/html/big.php /var/www/html/crash.php