HTTP 504 Gateway Timeout

Overview

The HTTP 504 Gateway Timeout status code indicates that the server, while acting as a gateway or proxy, did not receive a timely response from an upstream server or some other auxiliary server it needed to access in order to complete the request.

Purpose

The HTTP 504 response is used to inform the client that the server was acting as a gateway or proxy and did not receive a timely response from the upstream server.

Usage

Client Behavior:

  1. Send Request: The client sends an HTTP request to the server acting as a gateway or proxy.
  2. Receive Response: The client receives an HTTP 504 status code, indicating a gateway timeout.

Server Behavior:

  1. Gateway Timeout: The server acting as a gateway or proxy did not receive a timely response from the upstream server.
  2. Send Response: The server sends a 504 Gateway Timeout response to the client.

Scenarios

  • Upstream Server Delay: When the upstream server, accessed by the gateway, takes too long to respond.
  • Gateway or Proxy Timeout: The server acting as a gateway or proxy imposes a timeout waiting for a response from the upstream server.

Sequence Diagram

Illustrating the process for an HTTP 504 response:

sequenceDiagram
    participant Client
    participant Gateway as Gateway Server
    participant Upstream as Upstream Server

    Note over Client: Step 1: Client sends a request
    Client->>Gateway: GET /resource HTTP/1.1 (Step 1)

    Note over Gateway: Step 2: Gateway forwards request to upstream server
    Gateway->>Upstream: Forwarded Request (Step 2)

    Note over Upstream: Step 3: Upstream server does not respond in time
    Upstream-->>Gateway: No response (Step 3)

    Note over Gateway: Step 4: Gateway sends 504 Gateway Timeout to client
    Gateway-->>Client: HTTP/1.1 504 Gateway Timeout (Step 4)

Curl Request and Response Example

Sending a request using Curl to a gateway server that experiences a timeout:

curl -i http://example.com/resource
# Expected response: HTTP/1.1 504 Gateway Timeout

PHP cURL Request and Response Example

PHP script using cURL to handle a 504 Gateway Timeout response:

<?php
$ch = curl_init('http://example.com/resource');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 504) {
    echo "Gateway Timeout";
}
curl_close($ch);
?>

Python Request and Response Example

Python script sending a request to a gateway server that experiences a timeout and handling a 504 Gateway Timeout response:

import requests
response = requests.get('http://example.com/resource')
if response.status_code == 504:
    print("Gateway Timeout")

Apache Configuration for HTTP 504 Gateway Timeout

Configuring Apache to handle 504 errors:

# Apache Configuration Example
ErrorDocument 504 "Gateway Timeout"

NGINX Configuration for HTTP 504 Gateway Timeout

NGINX configuration for handling 504 errors:

# NGINX Configuration Example
error_page 504 /504.html;
location = /504.html {
    internal;
    return 504 "Gateway Timeout";
}

HTTP 503 Service Unavailable HTTP 505 HTTP Version Not Supported


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.