
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.
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:
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
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.
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.
A typo or wrong port in your Nginx or Apache proxy configuration can cause 502s immediately after a config change.
If your firewall is blocking the port your application listens on internally, your proxy can't reach it.
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.
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.
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;
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.
Make sure the port your app listens on (commonly 8000, 9000, 3000) is accessible internally:
sudo ufw status
sudo iptables -L
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.
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.
| Cause | Fix |
|---|---|
| App server crashed | Restart the process, check logs |
| Server overloaded | Scale up, optimise queries |
| Timeout too low | Increase proxy timeout settings |
| Bad proxy config | Review and correct nginx/apache config |
| Firewall blocking | Open 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.
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 moreCursor 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 moreClaude 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 moreLooking to monitor your website and domains? Join our platform and start today.