Expand description
A flexible and efficient reverse proxy implementation for Axum web applications.
This crate provides a reverse proxy that can be easily integrated into Axum applications, allowing for seamless forwarding of HTTP requests and WebSocket connections to upstream servers. It supports:
- Path-based routing
- Automatic retry mechanism
- Header forwarding
- Configurable HTTP client settings
- WebSocket proxying with:
- Automatic upgrade handling
- Bidirectional message forwarding
- Text and binary message support
- Proper close frame handling
- Easy integration with Axum’s Router
§Example
use axum::Router;
use axum_reverse_proxy::ReverseProxy;
// Create a reverse proxy that forwards requests from /api to httpbin.org
let proxy = ReverseProxy::new("/api", "https://httpbin.org");
// Convert the proxy to a router and use it in your Axum application
let app: Router = proxy.into();
You can also merge the proxy with an existing router, compatible with arbitrary state:
use axum::{routing::get, Router, response::IntoResponse, extract::State};
use axum_reverse_proxy::ReverseProxy;
#[derive(Clone)]
struct AppState { foo: usize }
async fn root_handler(State(state): State<AppState>) -> impl IntoResponse {
(axum::http::StatusCode::OK, format!("Hello, World! {}", state.foo))
}
let app: Router<AppState> = Router::new()
.route("/", get(root_handler))
.merge(ReverseProxy::new("/api", "https://httpbin.org"))
.with_state(AppState { foo: 42 });
§WebSocket Support
The proxy automatically detects WebSocket upgrade requests and handles them appropriately:
use axum::Router;
use axum_reverse_proxy::ReverseProxy;
// Create a reverse proxy that forwards both HTTP and WebSocket requests
let proxy = ReverseProxy::new("/ws", "http://websocket.example.com");
// WebSocket connections to /ws will be automatically proxied
let app: Router = proxy.into();
The proxy handles:
- WebSocket upgrade handshake
- Bidirectional message forwarding
- Text and binary messages
- Ping/Pong frames
- Connection close frames
- Multiple concurrent connections
Structs§
- Configuration options for the reverse proxy
- A reverse proxy that forwards HTTP requests to an upstream server.