pub struct ReverseProxy { /* private fields */ }
Expand description
A reverse proxy that forwards HTTP requests to an upstream server.
The ReverseProxy
struct handles the forwarding of HTTP requests from a specified path
to a target upstream server. It manages its own HTTP client with configurable settings
for connection pooling, timeouts, and retries.
Implementations§
Source§impl ReverseProxy
impl ReverseProxy
Sourcepub fn new<S>(path: S, target: S) -> Self
pub fn new<S>(path: S, target: S) -> Self
Creates a new ReverseProxy
instance with default options.
§Arguments
path
- The base path to match incoming requests against (e.g., “/api”)target
- The upstream server URL to forward requests to (e.g., “https://api.example.com”)
§Example
use axum_reverse_proxy::ReverseProxy;
let proxy = ReverseProxy::new("/api", "https://api.example.com");
Sourcepub fn new_with_options<S>(path: S, target: S, options: ProxyOptions) -> Self
pub fn new_with_options<S>(path: S, target: S, options: ProxyOptions) -> Self
Creates a new ReverseProxy
instance with custom proxy options and a default HTTP client configuration.
§Arguments
path
- The base path to match incoming requests againsttarget
- The upstream server URL to forward requests tooptions
- Custom configuration options for the proxy
§Example
use axum_reverse_proxy::{ReverseProxy, ProxyOptions};
let options = ProxyOptions {
buffer_bodies: true,
};
let proxy = ReverseProxy::new_with_options("/api", "https://api.example.com", options);
Sourcepub fn new_with_client<S>(
path: S,
target: S,
client: Client<HttpConnector, BoxBody<Bytes, Box<dyn Error + Send + Sync>>>,
) -> Self
pub fn new_with_client<S>( path: S, target: S, client: Client<HttpConnector, BoxBody<Bytes, Box<dyn Error + Send + Sync>>>, ) -> Self
Creates a new ReverseProxy
instance with a custom HTTP client and default proxy options.
Sourcepub fn new_with_client_and_options<S>(
path: S,
target: S,
client: Client<HttpConnector, BoxBody<Bytes, Box<dyn Error + Send + Sync>>>,
options: ProxyOptions,
) -> Self
pub fn new_with_client_and_options<S>( path: S, target: S, client: Client<HttpConnector, BoxBody<Bytes, Box<dyn Error + Send + Sync>>>, options: ProxyOptions, ) -> Self
Creates a new ReverseProxy
instance with a custom HTTP client and options.
This method allows for more fine-grained control over the proxy behavior by accepting a pre-configured HTTP client.
§Arguments
path
- The base path to match incoming requests againsttarget
- The upstream server URL to forward requests toclient
- A custom-configured HTTP clientoptions
- Custom configuration options for the proxy
§Example
use axum_reverse_proxy::{ReverseProxy, ProxyOptions};
use hyper_util::client::legacy::{Client, connect::HttpConnector};
use http_body_util::{combinators::BoxBody, Empty};
use bytes::Bytes;
use hyper_util::rt::TokioExecutor;
let client = Client::builder(TokioExecutor::new())
.pool_idle_timeout(std::time::Duration::from_secs(120))
.build(HttpConnector::new());
let proxy = ReverseProxy::new_with_client(
"/api",
"https://api.example.com",
client,
);
Trait Implementations§
Source§impl Clone for ReverseProxy
impl Clone for ReverseProxy
Source§fn clone(&self) -> ReverseProxy
fn clone(&self) -> ReverseProxy
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<S> From<ReverseProxy> for Router<S>
Enables conversion from a ReverseProxy
into an Axum Router
.
impl<S> From<ReverseProxy> for Router<S>
Enables conversion from a ReverseProxy
into an Axum Router
.
This implementation allows the reverse proxy to be easily integrated into an Axum application. It handles:
- Path-based routing using the configured base path
- State management using
Arc
for thread-safety - Fallback handling for all HTTP methods
§Example
use axum::Router;
use axum_reverse_proxy::ReverseProxy;
let proxy = ReverseProxy::new("/api", "https://api.example.com");
let app: Router = proxy.into();