Skip to main content

https_redirect

Function https_redirect 

Source
pub async fn https_redirect(req: Request, next: Next) -> Response
Expand description

Middleware that redirects plain-HTTP requests to HTTPS.

TLS terminates at a reverse proxy, so plain HTTP is detected through the X-Forwarded-Proto header (first value of a comma-separated list, case-insensitive). When it is http, the response is a 308 Permanent Redirect to https://{host}{path_and_query}, which keeps the method and body. The host comes from X-Forwarded-Host, falling back to Host. Requests without X-Forwarded-Proto (probes, worker traffic) and requests with no usable host pass through untouched.

ยงExamples

use axum::Router;
use axum::middleware::from_fn;
use axum::routing::get;
use ironflow_api::middleware::https_redirect;

let app: Router = Router::new()
    .route("/", get(|| async { "ok" }))
    .layer(from_fn(https_redirect));