Skip to main content

FastAPI Reverse Proxy

A robust, streaming-capable reverse proxy for FastAPI/Starlette with built-in Latency-Based Load Balancing and Active Health Monitoring.

Features

  • Async: Async by default.
  • Httpx Pool: Async HTTPX Pool for proxying.
  • Streaming Ready: Handles SSE (Server-Sent Events) and large payloads (such as big files) while keeping RAM usage low.
  • WebSocket Support: Seamless bidirectional tunneling with automated subprotocol negotiation.
  • Unified Load Balancing: Standard Round-Robin or Smart routing using a single utility.
  • Latency-Based Routing: Automatically routes traffic to the fastest healthy server (HEAD probe).
  • Advanced Overrides: Granular control over headers, body, and HTTP methods.
  • Smart Error Mapping: Automatically converts upstream connection failures into standard HTTP 502 (Bad Gateway) and 504 (Gateway Timeout) responses.
  • Resilient Handshakes: Customizable open_timeout for WebSockets to prevent proxy hangs during backend connection attempts.
  • Version Agnostic: Automatically handles websockets library version differences (12.0+ vs Legacy).

Quick Start

Use the lifespan handler as shown for an easy launch.

The simplest way to use the proxy is to use proxy_pass and/or proxy_pass_websocket on the endpoints.

from fastapi import FastAPI, Request, WebSocket
from contextlib import asynccontextmanager

from fastapi_reverse_proxy import Proxy, proxy_pass, proxy_pass_websocket

@asynccontextmanager
async def lifespan(app: FastAPI):
    async with Proxy(app):
        yield

app = FastAPI(lifespan=lifespan)

# catch-all route. recommended for a reverse proxy
@app.api_route("/{path:path}", methods=["GET","POST","PUT","DELETE"]) # don't forget to add the methods.
async def index(req: Request):
    """
    You always need to pass the "Request" object and to specify the host
    If you don't add a path, it will be the same as the original (/login --> http://127.0.0.1/login)
    """
    return await proxy_pass(req, "http://127.0.0.1:8080")

🛡️ Resilience & Error Handling

Error Handlingfastapi-reverse-proxy transforms upstream crashes into meaningful HTTPException responses (e.g., 502 Bad Gateway or 504 Gateway Timeout).

This allows you to implement custom failover logic, retry mechanisms, or specific error pages.

For a full implementation of a primary-to-backup failover system, see the example.

Advanced Examples:

Check examples for full examples, including:

  • Websocket Proxy
  • Socket.IO Proxy
  • Error Handling & Failover (examples/error_handling_example.py)

Advanced Proxying

The proxy_pass function and LoadBalancer.proxy_pass provide deep customization for upstream requests:

Parameter Type Description
timeout float Total request timeout in seconds (Default: 60.0).
method str Force a specific HTTP method (e.g., "POST").
override_body bytes Send custom data instead of the incoming request body.
additional_headers dict Append custom headers to the proxied request.
override_headers dict Use these headers instead of original request headers.
forward_query bool Whether to append the incoming query string (Default: True).
override_host str Override the outbound Host header sent to the target (useful for multi-host/virtual-hosting backends that key off the original requested host).

Monitoring & Configuration

HealthChecker (The Loop Owner)

The proactive component. It owns an internal asyncio background task that monitors backends.

  • Immediate Start: When you enter the async with block (or call start()), the checker performs an immediate check of all backends. This eliminates the "cold-start" window where backends are unknown.
  • Configuration Modes:
    • Standard: HealthChecker(["http://a", "http://b"], ping_path="/health")
    • Personalized: Pass a list of dictionaries for per-host settings:
      checker = HealthChecker([
          {"host": "http://api-1", "pingpath": "/v1/status", "maxrequests": 100},
          {"host": "http://api-2", "pingpath": "/health"}
      ])
      
  • Properties:
    • ping_path: Get or set the global health check path (default: "/").

LoadBalancer (The Decision Utility)

A normal Python object that makes routing decisions based on its source.

  • Stateful: While it has no background loop, it does track state (request counts for rate-limiting and the last time it pulled data from the health checker).
  • No Lifecycle Needed: It relies on the HealthChecker (or a static list) for data and doesn't need explicit start/stop calls.

WebSocket Refinements

The library implements "deferred negotiation" for WebSockets:

  1. The proxy receives the client's supported subprotocols from scope.
  2. It establishes an upstream connection first.
  3. Once the upstream accepts a protocol, the proxy calls websocket.accept(subprotocol=...) back to the client.
  4. This ensures the entire tunnel (Client <-> Proxy <-> Upstream) uses the same negotiated protocol.
  5. Handshake Timeout: Supports a customizable timeout parameter (default 10.0s) to prevent hangs if the backend is unresponsive.

Robustness & Safety

  • Termination Safety: Resource cleanup (closing httpx clients and sockets) is triggered even on task cancellation (BaseException).
  • Introspection-Based Compatibility: Uses inspect.signature to automatically detect version-specific parameters in the websockets library.
  • RFC 7230 Compliant Header Handling: Hop-by-hop headers (Connection, Transfer-Encoding, TE, Trailers, Keep-Alive, Proxy-Authenticate, Proxy-Authorization) are stripped from both outbound requests and responses, per spec. WebSocket handshake headers (Sec-WebSocket-Key, Upgrade, etc.) from the client are never forwarded to the target, avoiding handshake collisions.

Running Behind a Reverse Proxy (Nginx/Apache)

By default, proxy_pass and proxy_pass_websocket forward the client's original headers as-is — they do not set or rewrite X-Real-IP, X-Forwarded-For, X-Forwarded-Proto, or X-Forwarded-Host. If this library sits behind Nginx or Apache (the common setup), your upstream server is responsible for setting those headers before the request reaches this proxy:

location / {
    proxy_pass http://your-fastapi-app;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
}

For WebSocket routes, Nginx also needs explicit upgrade handling:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

location /ws/ {
    proxy_pass http://your-fastapi-app;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

Without this configuration, X-Forwarded-* headers will be empty or missing by the time they reach your application.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_reverse_proxy-0.3.1.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_reverse_proxy-0.3.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_reverse_proxy-0.3.1.tar.gz.

File metadata

  • Download URL: fastapi_reverse_proxy-0.3.1.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_reverse_proxy-0.3.1.tar.gz
Algorithm Hash digest
SHA256 5806414b427c644fe6ce983383832ad3df42d21fe8fb862f17fbc96f00e766ec
MD5 11d20e07d60d309269300ccf2a3708c3
BLAKE2b-256 3fc2c35d276df27107319b7ef54bd452fc527a6fcba91c9f4b7d6be47efa4c1b

See more details on using hashes here.

File details

Details for the file fastapi_reverse_proxy-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: fastapi_reverse_proxy-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_reverse_proxy-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 550c382ba67941e92fdefaee740fa8a354774fa4631069c82c9475baa691afee
MD5 68e8f484b666fdb93de63a115ce5946c
BLAKE2b-256 b0ceb4baa11e66ba2022f0975c327521740bcb15b13cb72a64edb1f4f311dcd9

See more details on using hashes here.

Release history Release notifications | RSS feed

0.4.0

2 files

This release

0.3.1 This release

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page