pub struct ReverseProxy { /* private fields */ }Expand description
Reverse proxy middleware.
Forwards incoming requests to one of the configured backends over HTTP/1.1.
On connection failure the next backend in the list is tried; when all
backends have failed the middleware returns 502 Bad Gateway.
Hop-by-hop headers are stripped before forwarding. X-Forwarded-For and
Via are added to every forwarded request.
Idle connections are pooled and reused across requests (up to
ConnPool::new_default limits: 8 idle per backend, 60-second timeout).
This eliminates per-request TCP handshake overhead and ephemeral-port
exhaustion. Use ReverseProxy::with_pool to share a pool across
multiple proxy instances or to tune pool parameters.
Implementations§
Source§impl ReverseProxy
impl ReverseProxy
Sourcepub fn new<I, S>(backends: I) -> Self
pub fn new<I, S>(backends: I) -> Self
Create a proxy that distributes requests across backends in
round-robin order. Each entry must be "http://host:port" or
"host:port" (port defaults to 80).
Sourcepub fn path_prefix(self, prefix: impl Into<String>) -> Self
pub fn path_prefix(self, prefix: impl Into<String>) -> Self
Only proxy requests whose URI starts with prefix.
Other requests are passed through to the next layer in the middleware chain (or the inner application).
Sourcepub fn strategy(self, _strategy: LoadBalancing) -> Self
pub fn strategy(self, _strategy: LoadBalancing) -> Self
Override the load balancing strategy (currently only RoundRobin).
Sourcepub fn connect_timeout_ms(self, ms: u64) -> Self
pub fn connect_timeout_ms(self, ms: u64) -> Self
Override the TCP connect timeout (default: 5 000 ms).
Sourcepub fn read_timeout_ms(self, ms: u64) -> Self
pub fn read_timeout_ms(self, ms: u64) -> Self
Override the response read timeout (default: 30 000 ms).
Sourcepub fn with_pool(self, pool: Arc<ConnPool>) -> Self
pub fn with_pool(self, pool: Arc<ConnPool>) -> Self
Attach a shared connection pool.
Useful for sharing one pool across multiple ReverseProxy instances
or for tuning pool parameters (capacity, idle timeout).
Sourcepub fn max_idle_conns(self, n: usize) -> Self
pub fn max_idle_conns(self, n: usize) -> Self
Set the maximum number of idle connections per backend (default: 8).