HTTP 502 Bad Gateway

Overview

The HTTP 502 Bad Gateway status code indicates that a server acting as a gateway or proxy received an invalid response from an upstream server. In other words, the server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.

Purpose

The HTTP 502 response is used to indicate that the server, while acting as a gateway or proxy, received an invalid response from the upstream server.

Usage

Client Behavior:

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

Server Behavior:

  1. Gateway/Proxy Role: The server acts as a gateway or proxy and forwards the request to an upstream server.
  2. Invalid Response: The server receives an invalid response from the upstream server.
  3. Send Response: The server sends a 502 Bad Gateway response to the client.

Scenarios

  • Upstream Server Issues: When the server acting as a gateway or proxy encounters issues with the upstream server.
  • Temporary Unavailability: The upstream server might be temporarily unavailable.

Sequence Diagram

Illustrating the process for an HTTP 502 response:

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

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

    Note over Gateway: Step 2: Gateway forwards the request to the Upstream Server
    Gateway->>Upstream: GET /resource HTTP/1.1 (Step 2)

    Note over Upstream: Step 3: Upstream Server sends an invalid response
    Upstream-->>Gateway: Invalid Response (Step 3)

    Note over Gateway: Step 4: Gateway sends a 502 Bad Gateway response to the Client
    Gateway-->>Client: HTTP/1.1 502 Bad Gateway (Step 4)

Curl Request and Response Example

Sending a request using Curl to a server acting as a gateway with an invalid response:

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

PHP cURL Request and Response Example

PHP script using cURL to handle a 502 Bad Gateway 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) == 502) {
    echo "Bad Gateway";
}
curl_close($ch);
?>

Python Request and Response Example

Python script sending a request to a server acting as a gateway and handling a 502 Bad Gateway response:

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

Apache Configuration for HTTP 502 Bad Gateway

Configuring Apache to handle 502 errors:

# Apache Configuration Example
ErrorDocument 502 "Bad Gateway"

NGINX Configuration for HTTP 502 Bad Gateway

NGINX configuration for handling 502 errors:

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

HTTP 501 Not Implemented HTTP 503 Service Unavailable


 

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.