Blog

What Is a Bad Gateway Error?: HTTP 502 vs 504 for Understanding Gateway Failures

A bad gateway error means a middle server could not get a clean answer from another server. Your browser asked for a page. A gateway, proxy, load balancer, or CDN tried to fetch it. Something went wrong between those servers, so you got a 502 or 504 instead of the page. MDN describes 502 as a gateway receiving an invalid response from an upstream server [source: MDN].

TLDR: A 502 Bad Gateway means the gateway got a bad answer. A 504 Gateway Timeout means it got no answer fast enough. For example, if 10,000 shoppers hit a sale page and the app server freezes for 30 seconds, you may see a 504 spike. If the app server sends broken data back, you may see a 502 instead.

What is a gateway?

Think of the web as a restaurant.

You are the hungry customer. Your browser is your voice. The waiter is the gateway. The kitchen is the real server.

You ask for pizza. The waiter runs to the kitchen. If the kitchen gives the waiter a burned note instead of pizza, that feels like a 502. If the kitchen does not answer at all, that feels like a 504.

Annoying? Very. Simple? Also yes.

A gateway can be many things:

  • Reverse proxy, such as Nginx or Apache.
  • Load balancer, which spreads traffic across servers.
  • CDN, such as a caching network.
  • API gateway, which routes app requests.
  • Firewall, which checks traffic before it hits the app.

These tools sit between you and the app. When they cannot talk to the app properly, they complain with gateway errors.

HTTP 502 vs 504: the tiny difference that matters

502 Bad Gateway means the gateway got a reply, but the reply was bad.

504 Gateway Timeout means the gateway waited, got bored, and gave up.

That is the key split.

  • 502: “I got an answer, but it makes no sense.”
  • 504: “I got no answer before the timer ran out.”

Honestly, it feels like being stuck between two support agents. One says, “We sent it.” The other says, “We never got it.” Meanwhile, your checkout button spins for 18 seconds and your coffee gets cold.

What causes a 502 Bad Gateway error?

A 502 often means one server replied with junk, crashed mid reply, or used the wrong protocol.

Common causes include:

  • App server crash: The backend died while handling the request.
  • Bad deployment: New code shipped with a broken route.
  • Wrong proxy settings: The gateway points to the wrong port or host.
  • DNS trouble: The gateway cannot find the correct upstream server.
  • SSL or TLS mismatch: One server expects HTTPS, the other uses HTTP.
  • Overloaded backend: The server replies with partial or invalid data.

A 502 is often sharp and sudden. One minute the site works. Then a release goes live. Then half the pages return errors. Classic.

What causes a 504 Gateway Timeout?

A 504 is usually slower and more boring. The gateway waited for the upstream server. The upstream server took too long. So the gateway quit.

Common causes include:

  • Slow database queries: A report page scans millions of rows.
  • Heavy traffic: Too many users hit the same server.
  • Third party API delays: Payment, shipping, or login services lag.
  • Long running scripts: A task takes 60 seconds, but the gateway timeout is 30 seconds.
  • Network issues: Servers can reach each other, but packets crawl.

Expect to waste time on this one if logs are messy. A 504 may hide the real slow part. It could be the app. It could be the database. It could be a tiny external API that takes 7 seconds longer than usual.

How users see these errors

Most users do not care about HTTP codes. Fair enough. They see a broken page.

A visitor may see messages like:

  • 502 Bad Gateway
  • 504 Gateway Timeout
  • Bad Gateway
  • Gateway Timeout
  • Cloud service unavailable
  • Something went wrong

On an online store, this can hurt fast. Say 4% of visitors reach checkout. If 25,000 people visit during a promo, that is 1,000 checkout users. If a 504 hits just 12% of them, 120 people may fail to pay. That is not a tiny bug. That is money walking out the door.

How to fix a 502 as a site owner

Start with the gateway logs. Then check the upstream app logs. Match the same timestamp.

Try this checklist:

  1. Check if the app server is alive. Restart it if it crashed.
  2. Review the latest deploy. Roll back if the error began after a release.
  3. Test the upstream directly. Use curl from the gateway server.
  4. Check ports and protocols. Make sure HTTP and HTTPS are not mixed up.
  5. Look at DNS. Confirm the gateway resolves the right address.
  6. Inspect response headers. Broken headers can trigger a 502.

If only one app server is bad, remove it from the load balancer. Let traffic hit the healthy ones. Then fix the sick server without pressure.

How to fix a 504 as a site owner

For a 504, chase time.

Find out where the request waits. App? Database? External API? Queue? File storage?

Use this checklist:

  1. Check slow logs. Database slow query logs are gold.
  2. Measure response time by layer. Gateway, app, database, API.
  3. Raise timeout limits carefully. Do not hide a bad query with a huge timer.
  4. Add caching. Cache pages, API results, or expensive database reads.
  5. Move long tasks to queues. Do not make users wait for background work.
  6. Scale the busy part. Add workers, app servers, or database capacity.

A bigger timeout can help for rare slow tasks. But it is not magic. If a query takes 45 seconds today, it may take 90 seconds tomorrow.

What can users do?

If you are just visiting the site, you have fewer options. Still, try the easy stuff.

  • Refresh the page. A short outage may clear in seconds.
  • Try again in a few minutes. Many gateway errors are temporary.
  • Check another device or network. Rule out local connection weirdness.
  • Clear browser cache. Rarely helps, but it is quick.
  • Do not spam checkout. You could create duplicate orders if the backend recovers.

If a payment page shows a 502 or 504, check your email and bank app before retrying. The payment may have gone through while the page failed. Yes, that is irritating.

Quick memory trick

Use this:

  • 502 = bad answer.
  • 504 = no answer in time.

That is the whole game.

A bad gateway error is not usually your browser being dramatic. It is a server in the middle saying, “I could not finish the job.” A 502 points to a broken or invalid upstream response. A 504 points to waiting too long. Fix the right one, and the web feels less haunted.

About Ethan Martinez

I'm Ethan Martinez, a tech writer focused on cloud computing and SaaS solutions. I provide insights into the latest cloud technologies and services to keep readers informed.