A robust, streaming-capable reverse proxy for FastAPI including WebSocket support.
Project description
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.
- Version Agnostic: Automatically handles
websocketslibrary 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")
Advanced Examples:
Check example.py for full examples, including
- Websocket Proxy
- Socket.IO Proxy
Advanced Proxying
The proxy_pass function and LoadBalancer.proxy_pass provide deep customization for upstream requests:
| Parameter | Type | Description |
|---|---|---|
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). |
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 withblock (or callstart()), 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"} ])
- Standard:
- 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 explicitstart/stopcalls.
WebSocket Refinements
The library implements "deferred negotiation" for WebSockets:
- The proxy receives the client's supported subprotocols from
scope. - It establishes an upstream connection first.
- Once the upstream accepts a protocol, the proxy calls
websocket.accept(subprotocol=...)back to the client. - This ensures the entire tunnel (Client <-> Proxy <-> Upstream) uses the same negotiated protocol.
Robustness & Safety
- Termination Safety: Resource cleanup (closing
httpxclients and sockets) is triggered even on task cancellation (BaseException). - Introspection-Based Compatibility: Uses
inspect.signatureto automatically detect version-specific parameters in thewebsocketslibrary.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_reverse_proxy-0.2.0.tar.gz.
File metadata
- Download URL: fastapi_reverse_proxy-0.2.0.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f6662c497cf589fe121fce3573a8ae04016bd50ae568009e7046cfe9678ee25
|
|
| MD5 |
5557b69087a832f6c13908c51d964157
|
|
| BLAKE2b-256 |
39238e6d5a7e653bec67a5fd898b687659001002600bbd58cb91989c1ab06ad0
|
Provenance
The following attestation bundles were made for fastapi_reverse_proxy-0.2.0.tar.gz:
Publisher:
python-publish.yml on tfsantos05/fastapi-reverse-proxy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_reverse_proxy-0.2.0.tar.gz -
Subject digest:
7f6662c497cf589fe121fce3573a8ae04016bd50ae568009e7046cfe9678ee25 - Sigstore transparency entry: 1450326178
- Sigstore integration time:
-
Permalink:
tfsantos05/fastapi-reverse-proxy@3489ca3b4c8333507b51023e0188432595de04e7 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/tfsantos05
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@3489ca3b4c8333507b51023e0188432595de04e7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_reverse_proxy-0.2.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_reverse_proxy-0.2.0-py3-none-any.whl
- Upload date:
- Size: 12.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
228f236fc114f6e18c7eb76cad23c2199dea6d4eec5cb306d4b36b8018e313cf
|
|
| MD5 |
4da833acdde612256c35a807eaa744b5
|
|
| BLAKE2b-256 |
21a180fe0d30b9d9469e53f13b2994a430f9e8c6269135170b0587d43fad9775
|
Provenance
The following attestation bundles were made for fastapi_reverse_proxy-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on tfsantos05/fastapi-reverse-proxy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_reverse_proxy-0.2.0-py3-none-any.whl -
Subject digest:
228f236fc114f6e18c7eb76cad23c2199dea6d4eec5cb306d4b36b8018e313cf - Sigstore transparency entry: 1450326329
- Sigstore integration time:
-
Permalink:
tfsantos05/fastapi-reverse-proxy@3489ca3b4c8333507b51023e0188432595de04e7 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/tfsantos05
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@3489ca3b4c8333507b51023e0188432595de04e7 -
Trigger Event:
release
-
Statement type: