Loading...

For 502 503 504 errors, each code tells you something different about a failed request. A 502 Bad Gateway means a gateway or proxy got an invalid response from the server behind it. In practice, proxies also return 502 when they can't connect to that server at all. A 503 Service Unavailable means the service can't take requests right now, because of overload, maintenance or running out of capacity. A 504 Gateway Timeout means the gateway waited too long for an answer and gave up. You can catch all three early by combining HTTP/S monitoring, response-time tracking and multi-region checks, then tuning your alert rules to match how your service is used.
That's the short version. The rest of this guide goes deeper: what each code means in practice, what usually causes it, and which monitor type is likely to warn you first. Grab a cuppa and let's work through it together. ☕
Picture this. It's 2am, your phone buzzes, and the site you shipped yesterday is showing a plain white page that says 'Bad Gateway'. Where do you look first? Your app? The database? The load balancer? Panic makes every layer look equally guilty.
The status code is your first clue, because it tells you what the gateway experienced when it tried to talk to whatever sits behind it.
| Code | Who is complaining | What it usually signals | Typical first check |
|---|---|---|---|
| 502 Bad Gateway | Proxy or load balancer | Upstream sent an invalid response. Many proxies also use 502 when the upstream connection fails outright, such as a crashed app or a wrong port or socket | Is the app process running, and does the proxy point at the right upstream? |
| 503 Service Unavailable | The server or an intermediary | Reachable, but can't take the request right now (overload, maintenance mode, exhausted workers, rate limiting, no healthy targets) | Capacity, worker and connection pool usage, maintenance flags |
| 504 Gateway Timeout | Proxy or load balancer | Upstream didn't answer in time (slow queries, hung processes, network trouble) | Slow queries, dependency latency, timeout settings at each layer |

Here's the line I like to remember: a 502 is a bad answer, and a 504 is no answer in time. With a 502, the proxy dealt with the upstream and the result wasn't usable. With a 504, the proxy sat there waiting until the clock ran out.
There's a wrinkle, though. The HTTP standard (RFC 9110) defines 502 as the gateway receiving an invalid response from an inbound server, 503 as a temporary inability to handle the request, and 504 as no timely response from upstream. A refused connection isn't an HTTP response at all, but proxies such as Nginx commonly report it as a 502 anyway. So when you see a 502, read it as 'the proxy couldn't get a usable answer from upstream'. Don't assume the upstream actually replied with something malformed. Exact behaviour varies by proxy and cloud provider.
Once you know which code you're looking at, the list of suspects gets much shorter. These are the culprits I see most often.
Most of us have lived through this one. You roll out a new version, the old containers shut down, and the new ones take twenty seconds to warm up. For those twenty seconds, the load balancer sends requests to something that isn't listening yet. The result is a short, sharp burst of 502s, and then everything looks fine. Nobody notices unless something happens to be watching at that moment. That's exactly why it's worth fixing with proper health checks and graceful shutdowns instead of shrugging it off.
The same underlying issue can surface as different codes depending on timing and configuration. A deploy that leaves a load balancer with no healthy targets might produce 503s. If the targets stay reachable but the app or a dependency becomes slow, those same requests might exceed the proxy timeout and turn into 504s instead. The code tells you what the layer reporting the error saw, not necessarily what broke.
That also means the same code can come from your app, a reverse proxy, a load balancer or a CDN. An edge-generated 504, for example, often means the origin was too slow, not that the edge itself failed. For exact definitions, keep RFC 9110 (sections 15.6.3 to 15.6.5), the Nginx docs and your cloud provider's troubleshooting pages bookmarked.
This is the part I find most useful day to day. Knowing the cause is nice, but knowing which monitor is likely to speak up first is what shortens your next incident.
One caveat before the table. 'First' depends on your check interval, where your probes run, your alert thresholds and which layer is affected. A monitor checking every minute will usually beat one checking every ten, whatever the type. Treat the table as a sensible starting point, not a guarantee. It also helps to separate monitors that spot the symptom (an HTTP check failing) from those that hint at the cause (a DNS change, an expiring certificate, a missed heartbeat).
| Error | Likely culprit | Early signal to look for | Useful monitor (symptom or cause) |
|---|---|---|---|
| 502 | Crashed app or dead workers | Non-2xx status, then missed background jobs | HTTP/S monitoring with status code checks (symptom), plus heartbeat monitoring for workers that die alongside the app (cause) |
| 502 / 503 | Failed config change or error page served oddly | Page loads but content is wrong or missing | Keyword monitoring to confirm expected content actually renders (symptom) |
| 503 | Resource exhaustion | Response times climbing before collapse, where the resource is the bottleneck | Response-time analytics on key endpoints (symptom), plus port checks to confirm the service is listening |
| 504 | Slow dependency | A specific endpoint slowing down | API monitoring on the endpoint that calls the dependency, with response-time thresholds (symptom) |
| 502 / 504 | DNS or certificate problems at the edge | Resolution changes, handshake failures, approaching expiry | DNS monitoring and SSL certificate monitoring (cause) |
| 504 (sometimes later) | Silently stalled scheduled task | A missed check-in, possibly hours earlier | Cron job monitoring and heartbeat checks (cause) |

A few notes on why these pairings work:
And to be honest with you, monitoring points you to the layer. Your logs and traces confirm the root cause. Think of monitors as the smoke alarm and logs as the fire investigation.
'Why do 503 errors come and go?' is one of the questions I hear most. The answer is that 503s are usually tied to capacity or state, and both change minute by minute. Load spikes, one unhealthy node behind a load balancer (so every third request fails), autoscaling lag, rate limiting kicking in and out, and short deploy windows can all do it.
This is where check frequency matters. If your monitor runs every 10 minutes and you have a 90-second burst of errors, there's a good chance it slips straight between two checks and you never hear about it. For critical endpoints, shorter intervals genuinely change what you can see.
Location matters too. A CDN edge or routing problem might only affect users in one region. A single monitoring location can miss that completely, or report a local blip as a total outage. Checking from several regions helps you tell a problem with one probe or network route from a broader outage, and Moonitor supports multi-region verification for exactly this reason.
There's a trade-off, though. If you only alert when several regions agree, a real incident confined to one region can be delayed or never alert at all. If most of your users are in the UK and your monitoring only confirms failures when a probe elsewhere also sees them, you could miss the very outage your customers are living through. Choose your confirmation rules based on where your users are and how much delay you can tolerate.
A few habits help here:
Here's a story that sticks with me. Imagine a small team that kept writing off a handful of evening 503s as a 'random glitch'. Nobody could reproduce it. Then they looked at their incident history and saw the errors lining up almost to the minute with a nightly data export that hogged the database. The glitch wasn't random at all. It ran on a schedule, and the history was what revealed it. Once they moved the job and added a heartbeat monitor on it, the 'mystery' was gone.
Good alerting means getting the right notifications, not more of them. Here's a setup that works well for many teams.
A gentle reminder: a quiet pager is a good thing, not a sign that monitoring isn't working. When alerts are rare and accurate, people trust them and respond faster when one arrives. That trust is hard to win back once a noisy week has taught everyone to ignore the channel.
Here's something that changed how I think about 504s: they often don't arrive out of nowhere. Response times frequently creep upward first, as a database, queue or dependency starts to strain. By the time users see a Gateway Timeout, the warning signs have sometimes been sitting in your graphs for hours. That said, not every timeout gives notice. A hung dependency or a sudden network fault can cause 504s with no gradual climb, so treat trends as a useful clue and not a guarantee.
A healthy baseline looks fairly flat, with small wobbles around a typical value. A problem looks like a slow climb, or a growing number of spikes. Averages can hide this, because a few very slow requests get smoothed away. Where you can, look at the spikes and the slow tail, not just the mean.

Some practical ways to use this:
Short answer: it narrows things down quickly, but it won't replace logs and tracing for the final root cause. The trick is to combine monitor types, because each one tests a different layer.

Here's how I read the signals:
When you're tired and the pressure is on, a fixed order stops you jumping around. Work from the outside in:
Each step either clears a layer or points you at the culprit.
You don't need a grand monitoring strategy to get going. Here's a sensible first pass, focused on what to watch. The alert routing and rules above cover how to act on it.
Monitors are usually quick to create, so you can cover a real project in an afternoon. Moonitor offers a 7-day free trial with no credit card needed, so you can try it on live services and see whether it fits. Do check the current plan details and support options on the Moonitor site to make sure they suit your team.
A 502 means the gateway got an invalid response from the upstream server. Many proxies also return 502 when they can't connect to the upstream at all, for instance because the app has crashed. A 504 means the gateway didn't get a response before its timeout expired. In practice, 502 often points to a crashed or misconfigured app, while 504 points to something slow or hung.
Because they are usually tied to capacity or state. Traffic spikes, exhausted workers, one unhealthy node behind a load balancer, deploys and rate limiting can all cause brief bursts of 503s that clear on their own. Frequent checks help you catch these short windows, and multi-region checks help you tell a local blip from a wider problem.
Expecting a 2xx response will usually flag 3xx and 4xx responses as failures too, so it isn't a 5xx-only rule. Instead, set your HTTP/S monitor to accept every status below 500 (or use a 'fail on 500 and above' condition if available). Handle redirects and client errors with separate monitors where they matter. Then add consecutive-failure rules and, where it suits your user base, multi-region confirmation to cut down on momentary blips.
It can point you in the right direction. Combining ping, port, HTTP, keyword and heartbeat monitors shows whether the network, proxy, application or a background job is the likely culprit. You will still want logs and traces to confirm the exact root cause.
Yes. Brief 502s during a rolling restart may be expected, though they are worth fixing with health checks and graceful shutdowns. Random 502s outside deploy windows deserve more urgency, as they usually signal crashes or resource problems.