Skip to content
Log In Sign Up info@muvcloud.com

What 502 Bad Gateway Means

A gateway or proxy is a server that receives a request and forwards it to another server behind it, called the backend or upstream. A 502 response says the proxy did its part, but the backend refused the connection, closed it early, or sent something that is not a valid HTTP response.

Typical pairs are Nginx in front of PHP-FPM, Nginx or Apache in front of a Node.js, Python, or Java application, a load balancer in front of web servers, and a CDN in front of the whole server. The fix is almost always on the backend side. For how the proxy layer works, see what is a reverse proxy.

What Visitors Can Try

  • Reload after a minute: a backend that is restarting often answers again shortly.
  • Another browser or a private window: rules out a stale cached error page.
  • Another network: try a mobile connection, and switch off a VPN or proxy extension.
  • Restart the router if several sites fail at once.

If the error stays for everyone, only the person who runs the server can fix it.

Step 1: Read the Proxy's Error Log

The proxy writes the reason next to every 502. On Nginx, and on Apache for Debian and Ubuntu:

sudo tail -n 50 /var/log/nginx/error.log
sudo tail -n 50 /var/log/apache2/error.log

On AlmaLinux and Rocky Linux the Apache log is /var/log/httpd/error_log. Look for lines that mention the upstream. The exact wording varies; these are the key phrases:

The log mentionsLikely cause
Connection refusedBackend stopped or wrong port
No such fileWrong or missing socket path
Permission deniedSocket permissions or SELinux
Prematurely closedBackend crashed mid-request

More on reading the log is in Nginx logs.

Step 2: Is the Backend Running?

sudo systemctl status php8.3-fpm
sudo ss -ltnp
sudo ss -lxp | grep php

On Debian and Ubuntu the PHP-FPM service carries the PHP version in its name; on AlmaLinux and Rocky Linux it is php-fpm. ss -ltnp lists TCP ports and the process on each, and ss -lxp lists Unix sockets. Then ask the backend directly, without the proxy:

curl -I http://127.0.0.1:3000

If that fails too, the problem is the application, not the proxy.

Step 3: Check the Address the Proxy Uses

The proxy and the backend must agree on one address. A fastcgi_pass unix:/run/php/php8.3-fpm.sock line in Nginx must match the listen line of the PHP-FPM pool, and a proxy_pass http://127.0.0.1:3000 line must match the port the application listens on. On Debian and Ubuntu the socket name carries the PHP version, so after a PHP upgrade the socket changes while the old name stays in the proxy configuration. The pool settings are explained in PHP-FPM.

Step 4: Was the Backend Killed?

sudo journalctl -u php8.3-fpm --since "1 hour ago"
sudo journalctl -k | grep -i "killed process"
free -h

When memory runs out, the kernel's OOM killer stops a process, often the backend, and every request to it then fails until it restarts. journalctl -k covers the current boot only. See OOM killer in Linux for how to confirm it and how to size workers to the memory you have.

Step 5: Firewall and SELinux Between the Two

When the backend runs on another server, its firewall must accept the proxy's address on the backend port. Test from the proxy with the same curl command, using the backend's private address.

On AlmaLinux and Rocky Linux, SELinux stops the web server from opening network connections to most backend ports unless a boolean allows it; the log then shows "Permission denied". Turn on that one permission instead of disabling SELinux:

sudo setsebool -P httpd_can_network_connect 1

When a CDN Sits in Front

A CDN shows its own error page when it cannot get a valid answer from your server. Cloudflare uses its own codes for this; see error 520 and error 521. To test your server while bypassing the CDN:

curl -I --resolve example.com:443:203.0.113.10 https://example.com

After the Fix

Restart the backend, check the proxy configuration with sudo nginx -t before reloading it, and make sure the backend starts at boot with systemctl enable. For the Nginx-specific causes in more depth, see 502 Bad Gateway in Nginx.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations