HTTP error reference

502 Bad Gateway

A 502 Bad Gateway means a server acting as a gateway or proxy, such as Nginx, a load balancer or Cloudflare, passed your request on to another server and got back an invalid response, or none at all. The proxy is working. The application behind it is down, unreachable or crashing mid-response.

If you are visiting the site

If you are only visiting the site, there is little you can fix. Reload after a minute, and try another network if the problem lasts. A 502 is almost always on the site’s side, not your browser’s.

Common causes

The application is not running

PHP-FPM, Node.js, Gunicorn or another app server has stopped or crashed, so the proxy has nothing to talk to. Nginx logs this as “connect() failed (111: Connection refused)”.

The proxy points at the wrong place

After an upgrade, the PHP-FPM socket path often changes (for example from php8.3-fpm.sock to php8.4-fpm.sock) while fastcgi_pass still names the old one. A wrong port in proxy_pass has the same effect.

Socket permissions

Nginx can see the PHP-FPM socket but is not allowed to open it, which shows up as “(13: Permission denied)”. The listen.owner and listen.group settings in the FPM pool must match the user Nginx runs as.

The application crashed mid-request

A worker that runs out of memory is killed by the operating system, and the proxy logs “upstream prematurely closed connection”. Check the kernel log for out-of-memory kills.

Response headers too large

Big cookies or many headers from the application can overflow the proxy’s buffer. Nginx logs “upstream sent too big header” and returns a 502.

How to fix it

502 Bad Gateway on Nginx

  • Read the error log first (often /var/log/nginx/error.log or the site’s own log). The message after “upstream” tells you which of the causes above you have.
  • As a reverse proxy in front of Node, Python or Docker, check the app listens on the address in proxy_pass. An app bound to another port, or a container Nginx cannot reach, returns “Connection refused”.
  • Check the app server is running, for example systemctl status php8.4-fpm, and start it if it is not.
  • Make sure fastcgi_pass or proxy_pass matches the socket or port the app actually listens on.
  • For “too big header”, raise fastcgi_buffer_size or proxy_buffer_size (the default is 4k or 8k), then reload Nginx.

On Ubuntu: nginx/1.18.0 or nginx/1.24.0

  • The version line only tells you the Ubuntu release: its own Nginx package is 1.18.0 on Ubuntu 20.04 and 22.04, and 1.24.0 on Ubuntu 24.04. The 502 comes from whatever runs behind Nginx.
  • After a release upgrade, check the PHP socket first. Each Ubuntu LTS ships a different PHP (7.4 on 20.04, 8.1 on 22.04, 8.3 on 24.04), so the upgrade removes the old socket while fastcgi_pass in your site file still points at it. Run ls /run/php/ to see which socket exists, then update the site file in /etc/nginx/sites-available/.
  • Run sudo tail -n 50 /var/log/nginx/error.log, and sudo systemctl status php8.3-fpm with your own PHP version. On Ubuntu, Nginx and PHP-FPM both run as www-data by default, so “Permission denied” on the socket usually means a pool file was changed to a different user.
  • Test the configuration with sudo nginx -t, then apply it with sudo systemctl reload nginx.

Apache

  • With mod_proxy, look for “proxy: error reading status line from remote server” or “connection refused” in the error log.
  • Confirm the backend in ProxyPass or the PHP-FPM handler is running and reachable.

Cloudflare

  • Look at the error page. Cloudflare says a Cloudflare-branded 502 came from your own server, while a blank, unbranded one came from Cloudflare. For a branded page, test your server directly, bypassing Cloudflare, to see the real error.
  • Cloudflare’s own 520 to 527 codes point to more specific problems, such as 521 (web server is down) and 522 (connection timed out).

WordPress and PHP

  • Look in the PHP-FPM log for “server reached pm.max_children” or workers being killed.
  • Check whether one plugin or import uses a lot of memory; WP_DEBUG_LOG can show the last error before the crash.

Quick checklist

  1. Check which proxy returned the error.

  2. Inspect upstream process health, listening address and socket permissions.

  3. Compare proxy error logs with application logs before changing timeouts.

Questions about 502 errors

What does “502 Bad Gateway nginx/1.18.0 (Ubuntu)” mean?

Nginx is working and the version line only names the server software. Nginx could not get a valid answer from the application behind it, usually PHP-FPM or a Node or Python app that is stopped or crashing. Its error log names the cause.

Is a 502 my fault or the website’s?

Nearly always the website’s. The gateway received a bad response from a server behind it. Reloading later is the only fix a visitor has.

What is the difference between 502 and 504?

A 502 means the server behind the proxy gave an invalid answer or closed the connection. A 504 means it never answered in time. A 502 points to something broken; a 504 usually points to something slow.

Does a 502 hurt SEO?

A short outage does not. Google slows crawling when it sees 5xx errors and keeps already indexed pages for a while, but pages that keep failing are eventually dropped.

Use the guided checklist