Home FixHow to Fix the 502 Bad Gateway Error on Nginx

How to Fix the 502 Bad Gateway Error on Nginx

by Knowledge Base Linux
How to Fix the 502 Bad — How to Fix the 502 Bad Gateway Error on Nginx

Few errors are as frustrating as the 502 bad gateway error. Your website was working a minute ago, and now every visitor sees a plain error page while Nginx itself appears to be running fine. The good news is that a 502 bad gateway on Nginx almost always has a logical, fixable cause — you just need to know where to look. In this guide we explain what the error means, the most common causes behind it, and the exact diagnostic steps to get your site back online. This article covers How to Fix the 502 Bad in detail.

What Does a 502 Bad Gateway Error Mean? – How to Fix the 502 Bad

Nginx usually works as a reverse proxy: it receives the visitor’s request and forwards it to an upstream (backend) server such as PHP-FPM, Apache, Node.js or Gunicorn. A 502 bad gateway means Nginx received an invalid response from that upstream — or no response at all. Nginx is the messenger here; the real problem is almost always in the backend, the connection between Nginx and the backend, or resource limits on the server.

Checking server logs to diagnose a 502 bad gateway error on Nginx

Common Causes of the 502 Bad Gateway Error – How to Fix the 502 Bad

  • PHP-FPM is down or crashed — the most frequent cause on WordPress and PHP sites.
  • Socket mismatch — Nginx points to a PHP-FPM socket or port that does not exist (common after a PHP version upgrade, e.g. php7.4-fpm.sock vs php8.2-fpm.sock).
  • The backend is overloaded — CPU or RAM is exhausted, so PHP-FPM workers cannot accept new requests.
  • Upstream timeouts — a slow script runs longer than proxy_read_timeout or fastcgi_read_timeout allows.
  • Buffer limits too small — large headers or cookies trigger “upstream sent too big header” errors.
  • Firewall or SELinux blocking local connections between Nginx and the backend.

Step 1: Read the Nginx Error Log First

Never guess — the error log tells you exactly which upstream failed and why. Check the last lines of the log:

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

Look for messages such as connect() failed (111: Connection refused) (backend not listening), upstream timed out (backend too slow), or no live upstreams (all backends down). The exact wording decides your next step.

Step 2: Check Whether PHP-FPM Is Running

If your site runs PHP, verify the PHP-FPM service and restart it if needed:

sudo systemctl status php8.2-fpm
sudo systemctl restart php8.2-fpm
sudo systemctl status nginx

Replace php8.2-fpm with your installed version. If PHP-FPM fails to start, read its own log at /var/log/php8.2-fpm.log — a full disk or a broken php.ini change is often the culprit.

Step 3: Verify the Socket or Port in Your Nginx Config

Open your site’s server block and check the fastcgi_pass directive:

grep -R "fastcgi_pass" /etc/nginx/sites-enabled/
ls -la /run/php/

The socket path in the config (for example unix:/run/php/php8.2-fpm.sock) must match a socket that actually exists in /run/php/. After a PHP upgrade, configs often still point to the old version’s socket — update the path, test the config with sudo nginx -t, and reload with sudo systemctl reload nginx.

Step 4: Check Server Resources

An overloaded server produces intermittent 502 errors under traffic. Check CPU, memory and disk:

top
free -h
df -h

If PHP-FPM workers are all busy, you may also see “server reached pm.max_children” in the PHP-FPM log. Raising pm.max_children in /etc/php/8.2/fpm/pool.d/www.conf helps only if the server has RAM to spare; otherwise optimize the application or upgrade the server. Installing a monitoring tool such as Netdata makes these bottlenecks visible before they take your site down — see our guide on installing Netdata for real-time Linux monitoring.

Step 5: Increase Timeouts and Buffers for Slow Backends

If the log shows timeouts or “upstream sent too big header”, adjust the relevant directives inside the location or server block:

proxy_connect_timeout 75s;
proxy_read_timeout 300s;
fastcgi_read_timeout 300s;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;

Reload Nginx afterwards. Treat this as treating the symptom — afterwards, profile the slow script so it does not need five minutes to answer.

How to Prevent 502 Bad Gateway Errors

Enable automatic restarts and monitoring for PHP-FPM, keep an eye on disk space and memory trends, and always run nginx -t before reloading after a config change. If you serve the site over HTTPS, our tutorial on securing Nginx with Let’s Encrypt SSL shows a clean, reliable Nginx server block you can compare yours against. The official Nginx proxy module documentation is the reference for every proxy and FastCGI directive mentioned here.

Quick Checklist: 502 Bad Gateway in Five Minutes

When the site is down and the pressure is on, run through this condensed sequence before anything else. First, reload the page in a private window to rule out a stale cached error. Second, tail the Nginx error log while reloading — the freshest line is your diagnosis. Third, confirm the backend service (PHP-FPM or your app) is running and listening on the exact socket or port your config names. Fourth, glance at top and df -h: resource exhaustion explains most “sudden” 502s on busy sites. Fifth, if you changed anything recently — a PHP upgrade, a new plugin, a config edit — revert that change mentally first; 502 errors appearing immediately after a deployment are the deployment until proven otherwise. Only after these five checks should you start tuning buffers and timeouts, because tuning first hides the real fault and guarantees the error returns under the next traffic peak.

Conclusion

A 502 bad gateway error means Nginx could not get a valid answer from your backend. Start with the error log, confirm PHP-FPM (or your app server) is actually running, verify the socket path, and check resources before touching timeouts. In the vast majority of cases, one of these five steps restores the site within minutes.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.