Skip to content

HTTP 502 Bad Gateway Error: What It Means and How to Fix It

What the HTTP 502 Bad Gateway error means and how to fix it on Nginx, Apache and PHP-FPM, with the exact log messages for each cause.

Category
Web Servers
Covers
Apache, Nginx, SELinux
On this page

An HTTP 502 Bad Gateway error means a server acting as a gateway or proxy got an invalid response, or no response at all, from the server behind it. In most setups the “gateway” is Nginx, Apache, a load balancer or a CDN such as Cloudflare, and the server behind it is PHP-FPM, a Node.js or Python app, or another web server.

This guide explains what the 502 status code means, the most common causes, and how to fix it on Nginx, Apache, PHP-FPM and app servers, with the exact log messages to look for.

What Does 502 Bad Gateway Mean?

The HTTP standard (RFC 9110) says a 502 means the server, “while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.”

In practice, a request often passes through several servers:

Browser → CDN / load balancer → Nginx (reverse proxy) → PHP-FPM / Node.js / Gunicorn

A 502 comes from one of the middle layers. It reached out to the next server and the connection was refused, dropped, or answered with something that wasn’t valid HTTP. The proxy itself is fine. The backend is the problem.

You may see it as:

  • 502 Bad Gateway or 502 Bad Gateway nginx
  • HTTP Error 502
  • Bad gateway Error code 502 (Cloudflare)
  • 502 Proxy Error (Apache mod_proxy)
  • HTTP Error 502.5 - Process Failure (IIS with ASP.NET Core)

502 vs 500, 503 and 504

CodeWhat happened
500 Internal Server ErrorThe application ran and crashed or hit an error. See our 500 error guide.
502 Bad GatewayThe proxy couldn’t get a valid response from the backend: it’s down, refused the connection or crashed mid-request.
503 Service UnavailableThe server is overloaded, rate-limiting or in maintenance. See our 503 guide.
504 Gateway TimeoutThe backend is running but too slow; the proxy gave up waiting. See our 504 guide.

Common Causes of a 502 Error

  1. The backend service is stopped or crashed: PHP-FPM, Node.js, Gunicorn, uWSGI or Tomcat isn’t running.
  2. Wrong socket path or port: Nginx points to php8.2-fpm.sock after you upgraded to PHP 8.3, or the app listens on a different port.
  3. Socket permission problems: Nginx can’t connect to the PHP-FPM Unix socket.
  4. The backend crashed during the request: a segfault, or the Linux OOM killer stopped the process because the server ran out of memory.
  5. Response headers too large for the proxy’s buffers (common with big cookies or many headers from WordPress plugins).
  6. SELinux blocking the proxy from connecting to a network port.
  7. The origin server is unreachable from the CDN: firewall rules, a changed IP or a down server behind Cloudflare.

Quick Fixes If You’re Visiting the Site

A 502 is a server-side problem, so visitors can’t fix it. These steps rule out short glitches:

  1. Reload the page after 30 to 60 seconds. Many 502s happen during a restart or deployment.
  2. Try another browser or a private window, or clear the site’s cookies.
  3. Check whether the site is down for everyone on its status page or social media.
  4. Come back later, or contact the site owner with the URL and time.

How to Fix a 502 Error on Your Own Server

Step 1: Read the proxy’s error log

The proxy logs exactly why it couldn’t talk to the backend. For Nginx:

sudo tail -n 30 /var/log/nginx/error.log

Match the message to its cause:

Nginx log messageMeaning
connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory)PHP-FPM isn’t running, or the socket path is wrong (Steps 2 and 3).
connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied)Nginx can’t use the socket (Step 4) or SELinux blocks it (Step 7).
connect() failed (111: Connection refused) while connecting to upstreamNothing is listening on that host and port (Steps 2 and 3).
upstream prematurely closed connection while reading response header from upstreamThe backend crashed or was killed during the request (Step 5).
upstream sent too big header while reading response header from upstreamProxy buffers too small (Step 6).
no live upstreams while connecting to upstreamEvery server in the upstream block is marked as failed.

For Apache as a reverse proxy, check /var/log/apache2/error.log for messages such as AH01102: error reading status line from remote server.

Step 2: Check that the backend is running

sudo systemctl status php8.3-fpm
sudo systemctl status myapp

If it’s stopped or failed, start it and read why it stopped:

sudo systemctl restart php8.3-fpm
sudo journalctl -u php8.3-fpm -n 50 --no-pager

A PHP-FPM config error stops it from starting. Test the config with sudo php-fpm8.3 -t.

Step 3: Match the socket path or port

The address in the Nginx config must match where the backend actually listens. Compare them:

grep -Rn "fastcgi_pass\|proxy_pass" /etc/nginx/sites-enabled/
grep -n "^listen" /etc/php/8.3/fpm/pool.d/www.conf
ls -l /run/php/
sudo ss -ltnp

After a PHP upgrade, the socket name changes (for example from php8.2-fpm.sock to php8.3-fpm.sock). Update fastcgi_pass, then test and reload Nginx:

sudo nginx -t && sudo systemctl reload nginx

For Node.js or Python apps, make sure the app listens on the same address as proxy_pass. An app bound to 127.0.0.1:3000 won’t answer on localhost:8080.

Step 4: Fix PHP-FPM socket permissions

The PHP-FPM pool decides who may connect to the socket. On Ubuntu and Debian, Nginx runs as www-data, and the pool should contain:

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

On RHEL-based systems, Nginx runs as nginx, so use that user or add it to listen.acl_users. Restart PHP-FPM after the change.

Step 5: Look for crashes and the OOM killer

If the log says the upstream “prematurely closed connection”, the backend died mid-request. Check if the kernel killed it for using too much memory:

sudo journalctl -k | grep -i "out of memory\|killed process"
free -h

If you see killed PHP-FPM, Node.js or database processes, add RAM or swap, lower pm.max_children so PHP-FPM can’t use more memory than the server has, or find the request that uses so much memory. See how to reduce PHP-FPM memory usage. PHP-FPM’s own log (/var/log/php8.3-fpm.log) records worker crashes such as exited on signal 11 (SIGSEGV).

Step 6: Increase buffers for large headers

For upstream sent too big header, give Nginx larger buffers for the response headers. For PHP-FPM:

fastcgi_buffer_size 32k;
fastcgi_buffers 16 16k;

For proxy_pass backends, use proxy_buffer_size and proxy_buffers with similar values. Put them in the server or location block, then test and reload Nginx.

Step 7: Allow proxy connections in SELinux

On RHEL, Rocky Linux, AlmaLinux and Fedora, SELinux stops Nginx and Apache from opening network connections to backends by default. If the log shows (13: Permission denied) while connecting to upstream for a TCP address, allow it:

sudo setsebool -P httpd_can_network_connect 1

Step 8: Check the CDN or load balancer

If the error page is branded by Cloudflare or your cloud provider, the CDN couldn’t reach your origin server. Check that the origin is up and answers directly (curl -I http://ORIGIN_IP -H "Host: example.com"), that your firewall allows the CDN’s IP ranges, and that the DNS record points to the right IP. For load balancers, check the target health checks: all targets failing their checks is a common cause.

For the full list of codes and what they mean, see our HTTP status codes list. Guides for other common errors:

Frequently Asked Questions

What does 502 Bad Gateway mean?

It means a proxy or gateway server (like Nginx or Cloudflare) couldn’t get a valid response from the server behind it. The backend is usually down, crashed or unreachable.

Is a 502 error my fault?

No. It’s a server-side problem. Reload after a minute; if it continues, the site owner needs to fix their backend.

How do I fix 502 Bad Gateway in Nginx?

Read /var/log/nginx/error.log. Then check that PHP-FPM or your app is running, that the socket path or port in fastcgi_pass or proxy_pass is correct, and that Nginx has permission to connect.

What is the difference between 502 and 504?

502 means the backend gave an invalid response or none at all (usually down or crashed). 504 means the backend is up but didn’t respond in time.

Why do I get 502 after upgrading PHP?

The PHP-FPM socket name includes the version, so Nginx still points to the old socket. Update fastcgi_pass to the new socket, for example /run/php/php8.3-fpm.sock, and reload Nginx.

Conclusion

An HTTP 502 Bad Gateway error means the front server couldn’t get a valid answer from the backend behind it. Visitors can only reload and wait. For site owners, the proxy’s error log tells you which of a few causes it is: a stopped backend, a wrong socket or port, socket permissions, a crash or out-of-memory kill, small buffers or SELinux. Check the log first, and most 502 errors take only a few minutes to fix.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top