Aarunya AppsAarunya Apps
504
Gateway Timeout
Server Error

HTTP 504 Gateway Timeout — nginx, Proxy Timeouts & Fix Guide

The server, acting as a gateway or proxy, did not receive a timely response from an upstream server. The connection to the upstream timed out — unlike 502 where the upstream returned an invalid response.

When to Return 504

Typically generated by nginx, load balancers, or CDNs when your application server takes too long. Return 504 from application code when calling an upstream service and it exceeds your timeout.

Common Causes

  • nginx proxy_read_timeout exceeded
  • Upstream database query taking too long
  • Third-party API call timing out
  • Slow downstream service in a microservice chain
  • Large file processing exceeding timeout

HTTP Response Example

HTTP/1.1 504 Gateway Timeout
Content-Type: application/json

{"error": "Gateway Timeout", "message": "Upstream did not respond in time"}

Code Examples

Express.js
// Call upstream with timeout
app.get('/data', async (req, res) => {
  const controller = new AbortController()
  const timeout = setTimeout(() => controller.abort(), 5_000)

  try {
    const response = await fetch('https://slow-upstream/data', {
      signal: controller.signal,
    })
    clearTimeout(timeout)
    res.json(await response.json())
  } catch (err) {
    clearTimeout(timeout)
    if (err.name === 'AbortError') {
      return res.status(504).json({ error: 'Gateway Timeout', message: 'Upstream timed out after 5s' })
    }
    res.status(502).json({ error: 'Bad Gateway' })
  }
})
Next.js App Router
export async function GET() {
  try {
    const response = await fetch('https://slow-api/data', {
      signal: AbortSignal.timeout(5_000), // 5s timeout
    })
    return Response.json(await response.json())
  } catch (err) {
    if (err instanceof DOMException && err.name === 'TimeoutError') {
      return Response.json({ error: 'Gateway Timeout' }, { status: 504 })
    }
    return Response.json({ error: 'Bad Gateway' }, { status: 502 })
  }
}

Related Status Codes

All HTTP status codes

Browse the complete HTTP status code reference.

All Status Codes

Frequently Asked Questions

What does HTTP 504 Gateway Timeout mean?

The server, acting as a gateway or proxy, did not receive a timely response from an upstream server. The connection to the upstream timed out — unlike 502 where the upstream returned an invalid response.

When should an API return 504?

Typically generated by nginx, load balancers, or CDNs when your application server takes too long. Return 504 from application code when calling an upstream service and it exceeds your timeout.

What causes an HTTP 504 error?

Common causes: nginx proxy_read_timeout exceeded; Upstream database query taking too long; Third-party API call timing out; Slow downstream service in a microservice chain; Large file processing exceeding timeout.