Home » Website Errors » 502 Bad Gateway: Find Which Server Is Failing and Why

502 Bad Gateway: Find Which Server Is Failing and Why

Published 2026-10-03 | Website Errors

502 Bad Gateway: Find Which Server Is Failing and Why banner

A 502 Bad Gateway comes from a server that sits in front of another one. It forwarded your request, and what came back was invalid or never came back at all. The front server (often nginx, Apache, a load balancer or a CDN) is working. The one behind it is not.

Step 1: find out who is showing the error

Look at the response headers:

curl -sI https://example.com

The server: header and the error page itself hint at the layer that produced the 502:

  • A page branded by your CDN means the CDN could not reach your origin server.
  • A plain "502 Bad Gateway, nginx" page means nginx could not get a good answer from your application.
  • A branded host error page on shared or managed hosting means the issue is on their side or in your app's process.

Step 2: bypass the front layer

If a CDN is involved, test the origin directly. If the origin returns the page, the problem is between the CDN and the origin (firewall, SSL mode, wrong origin address). If the origin also returns 502, continue below.

curl -sI --resolve example.com:443:ORIGIN_IP https://example.com

Replace ORIGIN_IP with your server's real IP address.

Step 3: read the error log of the proxy

The log names the exact failure. For nginx:

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

Match the message:

Log message Meaning Fix direction
connect() failed (111: Connection refused) while connecting to upstream Nothing is listening where nginx forwards to The app is down or on a different port
upstream prematurely closed connection The app accepted the request and then crashed or exited Check the app's own error log
no live upstreams All backends marked as down Fix the backends, or health check settings
connect() to unix:/run/php/php-fpm.sock failed (No such file or directory) PHP-FPM socket path is wrong or the service is stopped Start PHP-FPM, correct the socket path
upstream sent too big header Response headers exceed the proxy buffer Increase proxy_buffer_size

Step 4: check the application and its process

  • Is it running? sudo systemctl status your-app (or php8.2-fpm, pm2 list, docker ps).
  • Did it just crash from running out of memory? Check dmesg | grep -i "killed process" or your host's memory graphs. Out of memory kills are a frequent hidden cause.
  • Did a deploy change the port, socket or start command?
  • Restart it and watch its log while it starts. Errors during startup are the most informative.

Step 5: check the proxy configuration

Make sure the address nginx forwards to matches where the app really listens:

proxy_pass http://127.0.0.1:3000;

If the app listens on port 8000, every request returns 502. After any change, run sudo nginx -t before reloading.

WordPress and shared hosting

You usually cannot read server logs, so work from the top:

  1. Disable plugins (rename the wp-content/plugins folder over FTP or the file manager) and test again.
  2. Switch to a default theme.
  3. If a heavy request triggers it (import, backup, big page), PHP may be hitting its time or memory limit. Ask the host or raise the limits.
  4. If none of this helps, send the host the time of the error and the exact URL. They can read the logs you cannot.

Common mistakes

  • Restarting the web server when the application behind it is what failed.
  • Reading the access log instead of the error log. The access log only says that a 502 happened.
  • Fixing the symptom with larger timeouts when the app is simply crashing.
  • Assuming the CDN is at fault without testing the origin directly.

Frequently Asked Questions

Is a 502 error my website's fault or my host's fault?

The error comes from a server acting as a gateway or proxy (such as nginx or a CDN) that could not get a valid response from the server behind it. On a site you run yourself the cause is nearly always the application or process behind the proxy. On shared hosting, contact the host.

What is the difference between 502, 503 and 504?

502 means the upstream sent an invalid response or the connection failed. 503 means the service is unavailable, often overloaded or in maintenance. 504 means the upstream took too long to answer.

Will refreshing fix a 502?

Sometimes, if the upstream was restarting or briefly overloaded. If it keeps returning, something is down or misconfigured and a refresh will not help.

Found a mistake or still stuck? Contact us. See how we write and check these guides in our editorial policy, or read about DocumentGuy.

More in Website Errors