Module proxy_protocol

Source
Available on crate feature proxy_protocol only.
Expand description

This feature allows the hyper_server to be used behind a layer 4 load balancer whilst the proxy protocol is enabled to preserve the client IP address and port. See The PROXY protocol spec for more details: https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt.

Any client address found in the proxy protocol header is forwarded on in the HTTP forwarded header to be accessible by the rest server.

Note: if you are setting a custom acceptor, enable_proxy_protocol must be called after this is set. It is best to use directly before calling serve when the inner acceptor is already configured. ProxyProtocolAcceptor wraps the initial acceptor, so the proxy header is removed from the beginning of the stream before the messages are forwarded on.

§Example

use axum::{routing::get, Router};
use std::net::SocketAddr;
use std::time::Duration;

#[tokio::main]
async fn main() {
   let app = Router::new().route("/", get(|| async { "Hello, world!" }));

   let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
   println!("listening on {}", addr);

   // Can configure if you want different from the default of 5 seconds,
   // otherwise passing `None` will use the default.
   let proxy_header_timeout = Some(Duration::from_secs(2));

   hyper_server::bind(addr)
       .enable_proxy_protocol(proxy_header_timeout)
       .serve(app.into_make_service())
       .await
       .unwrap();
}

Structs§

ForwardClientIp
Middleware for adding client IP address to the request forwarded header. see spec: https://www.rfc-editor.org/rfc/rfc7239#section-5.2
ProxyProtocolAcceptor
Acceptor wrapper for receiving Proxy Protocol headers.