Browser showing a 502 Bad Gateway error message on a dark screen
# website errors# troubleshooting

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

Few things are more frustrating than your website returning a 502 Bad Gateway error. It looks broken, it's unclear who's to blame, and if you're running a business, every minute it's showing is costing you.

The good news: 502 errors are almost always fixable, and once you understand what's causing one it becomes a lot less mysterious.

What Does a 502 Bad Gateway Mean?

A 502 error means the server acting as a gateway or proxy received an invalid response from an upstream server.

In plain terms: your web server asked another server for something — maybe your application server, your PHP process, or a backend service — and got back garbage, nothing, or an error instead of a valid response.

It's different from a 500 Internal Server Error, which means something went wrong on the server itself. A 502 specifically points to a communication problem between two servers.

Common setups where this happens:

  • Nginx → PHP-FPM: Nginx receives a request and passes it to PHP-FPM to run your PHP code. If PHP-FPM crashes or is overwhelmed, Nginx returns a 502.
  • Nginx → Gunicorn/uWSGI: Python web apps running behind Nginx as a reverse proxy.
  • Cloudflare → your origin: Cloudflare sits in front of your server and can't get a valid response from it.
  • Load balancer → backend: A load balancer trying to reach backend servers that aren't responding.

Common Causes of a 502 Error

1. Your Application Server Has Crashed

The most common cause. PHP-FPM, Gunicorn, Node.js, or whatever process is running your app has died.

How to check:

# Check PHP-FPM
sudo systemctl status php8.2-fpm

# Check a Node/Python app via PM2
pm2 status

# Check your error logs
sudo tail -f /var/log/nginx/error.log

2. The Upstream Server Is Overloaded

If your application server is alive but flooded with requests, it may not respond in time and your proxy server gives up, returning a 502.

Signs: high CPU or memory usage, slow response times before the 502s started.

3. Timeout Values Are Too Low

If your application takes longer to respond than the proxy's timeout setting allows, you'll get a 502. This is common on servers running heavy database queries or file processing tasks.

4. Misconfigured Proxy Settings

A typo or wrong port in your Nginx or Apache proxy configuration can cause 502s immediately after a config change.

5. Firewall Blocking Internal Traffic

If your firewall is blocking the port your application listens on internally, your proxy can't reach it.

How to Fix a 502 Bad Gateway

Step 1: Check Your Application Process

Start here. Restart your app server and see if the error clears:

# Restart PHP-FPM
sudo systemctl restart php8.2-fpm

# Restart Nginx
sudo systemctl restart nginx

# Restart Apache
sudo systemctl restart apache2

Check your Nginx error logs or Apache error logs for clues — they'll usually tell you exactly what went wrong.

Step 2: Check Server Resources

A server that's run out of memory or CPU capacity can't process new requests. Run:

top
free -h
df -h

If RAM or disk is maxed out, that's likely your culprit.

Step 3: Increase Proxy Timeouts

If slow requests are causing the 502, increase your timeout settings in Nginx:

proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;

Step 4: Review Recent Changes

Did someone deploy new code, push a config change, or restart a service just before the 502s started? Roll back if needed and verify the issue is resolved.

Step 5: Check for Firewall Rules

Make sure the port your app listens on (commonly 8000, 9000, 3000) is accessible internally:

sudo ufw status
sudo iptables -L

Is It a Temporary Issue?

Sometimes 502 errors are brief — a server restart, a traffic spike, a brief network hiccup. If you refresh after a few seconds and it's gone, it was transient.

But if it keeps happening, you need to get to the root cause. Repeated 502s are a sign something in your infrastructure is unstable, and that means downtime that affects your SEO and your users' trust.

Prevent 502s With Uptime Monitoring

A 502 error that lasts hours without anyone noticing is a serious problem. Uptime monitoring means you'll get alerted the moment your site starts returning errors — not when a customer emails you to complain.

Domain Monitor checks your site every minute from multiple locations, so you'll know about a 502 within seconds. You can set up downtime alerts to get notified via email, SMS, or Slack the moment something goes wrong.

Quick Summary

CauseFix
App server crashedRestart the process, check logs
Server overloadedScale up, optimise queries
Timeout too lowIncrease proxy timeout settings
Bad proxy configReview and correct nginx/apache config
Firewall blockingOpen the internal port

A 502 is almost never a dead end — it's a signpost pointing you toward a specific problem. Follow the logs, check your processes, and you'll have it fixed in no time.

More posts

What Is Generative AI? How It Works and What It Creates

Generative AI creates new content — text, images, code, and more. This guide explains how it works, what tools are available, and where it's genuinely useful versus overhyped.

Read more
What Is Cursor AI? The AI Code Editor Explained

Cursor AI is an AI-powered code editor built on VS Code. Learn what it does, how it works, and whether it's the right tool for your development workflow.

Read more
What Is Claude Opus? Anthropic's Most Powerful Model Explained

Claude Opus is Anthropic's most capable AI model, built for complex reasoning and demanding tasks. Learn what it does, how it compares, and when to use it.

Read more

Subscribe to our PRO plan.

Looking to monitor your website and domains? Join our platform and start today.