You try to visit a website and see the error message "502 Bad Gateway". The page does not load and all you see is an error. In this detailed guide we explain what a 502 error means, what the possible causes are and how you can fix it as a visitor or as a website owner.

What is an HTTP 502 Bad Gateway error?

The HTTP 502 status code means that a server acting as a gateway or proxy received an invalid response from an upstream server. In plain language: there are several servers between you and the website, and the communication between those servers went wrong.

In a typical web hosting setup, it works like this:

  1. Your browser sends a request to the web server (Nginx or Apache)
  2. The web server forwards the request to the application server (PHP-FPM)
  3. PHP-FPM processes the page and sends the result back to the web server
  4. The web server sends the page to your browser

A 502 error occurs when step 3 fails: PHP-FPM crashes, returns an invalid response, or does not respond at all. The web server (the gateway) then cannot complete the request.

This is different from a 500 error (an internal server error in PHP) or a 503 error (server overloaded). A 502 is specifically about the communication between two servers.

Variations of the 502 error

The 502 error message can appear in different ways. As soon as you see the code 502, you know this guide applies.

Error messageDescription
502 Bad GatewayMost common variant
HTTP Error 502Standard HTTP error message
502 Proxy ErrorProxy-related variant
502 Server ErrorShortened variant
502 Bad Gateway - nginxNginx-specific variant
502 Bad Gateway - CloudflareCDN-related variant
502. That's an error.Google-specific display
Bad Gateway: The proxy server received an invalid responseExtended Apache variant

Fixing a 502 error as a visitor

As a visitor your options are limited, but you can try the following:

  1. Refresh the page: press F5 or Ctrl+R. A 502 error is often temporary.
  2. Wait 30 seconds to a minute: the server may restart automatically.
  3. Clear your browser cache: sometimes an old 502 response is shown from the cache.
  4. Try a different DNS: switch to Google DNS (8.8.8.8) or Cloudflare DNS (1.1.1.1).
  5. Check downdetector.nl: see whether other users are reporting the same problem.

Fixing a 502 error as a website owner

Do you own the website? Then follow these 8 steps to find and fix the cause.

1. Check whether PHP-FPM is running

The most common cause of a 502 error is that PHP-FPM has crashed or stopped responding. Check the status:

sudo systemctl status php8.2-fpm

If PHP-FPM has stopped, restart it:

sudo systemctl restart php8.2-fpm

On shared hosting you cannot restart PHP-FPM directly. In that case, contact your hosting provider or wait until the server recovers automatically.

2. Check the Nginx or Apache error log

The web server error log gives you exact information about why the request failed:

# Nginx error log
sudo tail -50 /var/log/nginx/error.log

# Apache error log
sudo tail -50 /var/log/apache2/error.log

Look for lines containing "502" or "upstream": they tell you exactly which upstream server did not respond and why.

Common log lines:

upstream prematurely closed connection while reading response header
connect() to unix:/run/php/php8.2-fpm.sock failed (111: Connection refused)
upstream timed out (110: Connection timed out) while reading response header

3. Increase the proxy timeout

If your website performs heavy operations (large database queries, image processing, imports), the web server may not wait long enough for PHP-FPM.

Nginx configuration:

proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
fastcgi_read_timeout 300;

Apache configuration:

ProxyTimeout 300
Timeout 300

After making the changes, restart the web server:

sudo systemctl restart nginx   # or apache2

4. Check PHP memory and process limits

PHP-FPM can crash if it has too little memory or tries to run too many processes:

Check the PHP-FPM pool configuration:

# Usually in /etc/php/8.2/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

If pm.max_children is too low for your traffic, PHP-FPM can no longer accept new requests. Increase this value or switch to pm = ondemand if your traffic fluctuates.

Also check the PHP memory limit in php.ini:

memory_limit = 512M

5. Test without a CDN or reverse proxy

If you use Cloudflare, Sucuri or another CDN/proxy, the problem may be there:

  • Temporarily disable the CDN (point DNS directly to your server)
  • Check the CDN dashboard for error messages
  • With Cloudflare: check that the SSL mode is set correctly (Full or Full Strict)
  • Test whether your website works through the server's direct IP address

If your website does work without the CDN, the problem is in the CDN configuration.

6. Check your DNS settings

Incorrect DNS settings can point your domain name to the wrong server, which results in a 502 error:

  • Check that your A record points to the correct server IP address
  • After DNS changes, wait 24 to 48 hours for full propagation
  • Use dig domein.nl or nslookup domein.nl to check your DNS

7. Restart the server

If restarting individual services does not help, a full server restart may be needed. This fixes problems with stuck processes, memory leaks or corrupted temporary files.

On a VPS or dedicated server:

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

Or a full restart:

sudo reboot

On shared hosting: contact your hosting provider for a restart.

8. WordPress-specific checks

For WordPress websites, these additional causes may play a role:

  • Heavy plugins: deactivate recently installed or updated plugins
  • WP-Cron overload: disable WP-Cron and use a real server cron job
  • Object cache conflict: if you use Redis or Memcached, check that the cache service is running
  • Heartbeat API: limit the WordPress Heartbeat API with the Heartbeat Control plugin

Preventing 502 errors

With these measures you minimize the chance of 502 errors:

  • Monitor your server: set up uptime monitoring with tools such as UptimeRobot or Pingdom
  • Scale your hosting: make sure your hosting plan has enough resources for your traffic
  • Optimize your code: make sure your database queries are efficient and avoid heavy operations during page loads
  • Use caching: a well-configured cache (Redis, Varnish or page cache) reduces the load on PHP-FPM
  • Keep software up to date: use the latest PHP version and keep your CMS and plugins updated
  • Configure auto-restart: set up systemd to restart PHP-FPM automatically after a crash

Frequently asked questions about the 502 error

What exactly does HTTP 502 Bad Gateway mean?

A 502 Bad Gateway error means that an intermediate server (a gateway or proxy) received an invalid or incomplete response from the server behind it. The problem is not in your browser or connection, but in the communication between two servers. Usually it involves a web server (Nginx/Apache) that does not get a valid response from PHP-FPM.

What is the difference between a 502 and a 504 error?

With a 502 error, the gateway receives an invalid response from the upstream server. With a 504 Gateway Timeout, the gateway receives no response at all within the configured time limit. A 502 points to a crash or error, a 504 to a timeout. The solutions overlap, but with a 504 you mainly need to increase the timeout settings.

Can a 502 error be caused by my hosting provider?

Yes, on shared hosting a 502 error can be caused by server problems that you have no control over. If the problem persists and you have gone through all the steps, contact your hosting provider. If 502 errors keep coming back, consider upgrading to a VPS for more control and dedicated resources.

Does a 502 error affect my SEO?

A single, brief 502 error has hardly any impact on your SEO. However, if Google repeatedly runs into a 502 while crawling your pages, this can lead to a lower crawl rate and, over time, a drop in your search rankings. Monitor your site with Google Search Console so you quickly find out about crawl problems.

How long does a 502 error usually last?

That depends on the cause. If PHP-FPM restarts automatically, a 502 error can be resolved within 30 seconds. With server problems or configuration errors, it can take longer until the administrator steps in. On well-configured servers, PHP-FPM crashes are detected and restarted automatically, which keeps downtime to a minimum.

Getting 502 errors regularly? See our web hosting plans with LiteSpeed, NVMe SSD and a stable PHP-FPM configuration. Or choose a VPS with full control over your server configuration.