> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Fix 502 Bad Gateway in Nginx

> Fix 502 Bad Gateway in Nginx: read /var/log/nginx/error.log, then start PHP-FPM or your app, fix the socket path or permissions, or allow it in SELinux.

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.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Intermediate | 5–15 minutes | Ubuntu, Debian, AlmaLinux / Rocky | SSH access with sudo |

## Before you start

* Connect with [SSH](https://help.freakhosting.com/portals/other/how-to-connect-to-a-linux-server-with-ssh.md). 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

```bash theme={null}
sudo tail -n 20 /var/log/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.

| Message in error.log | What it means | Fix |
| - | - | - |
| `connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory)` | PHP-FPM is not running, or the socket name is wrong | [Step 2](#step-2-start-the-backend) and [Step 3](#step-3-match-the-socket-or-port) |
| `connect() failed (111: Connection refused) while connecting to upstream` | Nothing listens on that port | Start the app, check the port |
| `connect() to unix:… failed (13: Permission denied)` | Nginx may not open the socket | [Step 4](#step-4-fix-permissions-and-selinux) |
| `(13: Permission denied) while connecting to upstream` on AlmaLinux / Rocky with a TCP port | SELinux blocks Nginx | [Step 4](#step-4-fix-permissions-and-selinux) |
| `upstream prematurely closed connection while reading response header from upstream` | The app crashed or closed the request | Check the app's own log |
| `upstream sent too big header while reading response header from upstream` | Response headers (often cookies) exceed the buffer | [Step 5](#step-5-raise-buffers-for-large-headers) |

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

> [tab] PHP-FPM (Ubuntu / Debian)
>
> ```bash theme={null}
> systemctl status 'php*-fpm'
> sudo systemctl restart php8.3-fpm
> ```
>
> Use your version, for example `php8.4-fpm` on Debian 13.

> [tab] PHP-FPM (AlmaLinux / Rocky)
>
> ```bash theme={null}
> sudo systemctl restart php-fpm
> systemctl status php-fpm
> ```

> [tab] Node.js / Python app
>
> ```bash theme={null}
> pm2 list                       # apps managed by PM2
> sudo systemctl status myapp    # apps run by systemd
> curl -I http://127.0.0.1:3000  # does the app answer at all?
> ```

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.

```bash theme={null}
ls -l /run/php/                         # sockets PHP-FPM created (Ubuntu / Debian)
sudo nginx -T | grep -E "fastcgi_pass|proxy_pass"
sudo ss -tlnp | grep 3000               # who listens on port 3000
```

A common cause after an OS upgrade or a [new PHP version](https://help.freakhosting.com/portals/other/how-to-install-multiple-php-versions.md): the config still says `php8.2-fpm.sock` while only `php8.3-fpm.sock` exists. Fix the path, then:

```bash theme={null}
sudo nginx -t && sudo systemctl reload nginx
```

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:

```bash theme={null}
ls -l /run/php/php8.3-fpm.sock
```

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](https://help.freakhosting.com/portals/other/how-to-deploy-a-python-app-with-gunicorn.md).

**SELinux (AlmaLinux / Rocky).** Allow Nginx to connect to network ports:

```bash theme={null}
sudo setsebool -P httpd_can_network_connect 1
```

> [!WARNING]
>
> Do not disable SELinux to fix a 502. Set the boolean above, and see [SELinux on AlmaLinux and Rocky](https://help.freakhosting.com/portals/other/how-to-use-selinux-on-almalinux-and-rocky.md) for other denials.

## Step 5: Raise buffers for large headers

For `upstream sent too big header`, add to the `server` or `location` block:

```nginx theme={null}
# reverse proxy
proxy_buffer_size 16k;
proxy_buffers 8 16k;

# PHP-FPM
fastcgi_buffer_size 16k;
fastcgi_buffers 8 16k;
```

Then `sudo nginx -t && sudo systemctl reload nginx`.

## How to check it is fixed

```bash theme={null}
curl -I -H "Host: example.com" http://127.0.0.1/
```

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](https://help.freakhosting.com/portals/other/how-to-fix-out-of-memory-killed-processes.md).

### 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.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Website not loading](https://help.freakhosting.com/portals/other/how-to-fix-website-not-loading-on-your-vps.md)
>
> Broader site checks.

> [card] [Install a LEMP stack](https://help.freakhosting.com/portals/other/how-to-install-a-lemp-stack.md)
>
> A working PHP-FPM setup.

> [card] [Nginx reverse proxy](https://help.freakhosting.com/portals/other/how-to-set-up-nginx-as-a-reverse-proxy.md)
>
> Correct proxy settings.

> [card] [Read logs with journalctl](https://help.freakhosting.com/portals/other/how-to-read-logs-with-journalctl.md)
>
> Find why a service failed.

***

> [!NOTE]
>
> **Checked against the official [nginx proxy module](https://nginx.org/en/docs/http/ngx_http_proxy_module.html) and [Red Hat Enterprise Linux 9 Nginx guide](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/deploying_web_servers_and_reverse_proxies/setting-up-and-configuring-nginx_deploying-web-servers-and-reverse-proxies)** 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.

> [!NOTE]
>
> ### 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](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.