Expand description
HTTP-to-HTTPS redirect middleware for Axum and Tower.
A tower::Layer that detects incoming HTTP requests and issues a
permanent redirect to the HTTPS equivalent. HTTPS requests are forwarded
to the inner service unchanged.
Detection follows this priority order:
X-Forwarded-Proto: http— set by most reverse proxies when the upstream connection is plain HTTP.- URI scheme of
http— present in direct (non-proxy) connections.
If neither indicator is present the request passes through unchanged, which is the safe default when the protocol cannot be determined.
§Features
- Configurable redirect status:
308 Permanent Redirect(default, method-preserving) or301 Moved Permanently(legacy compatibility). - Optional HTTPS port override for non-standard port setups.
- Graceful fallback: if no
Hostheader is present the request is forwarded rather than returning a malformed redirect withoutLocation.
§Quick Start
use axum::{routing::get, Router};
use rune_axum_redirect_https::RedirectHttpsLayer;
let app: Router = Router::new()
.route("/", get(|| async { "hello" }))
.layer(RedirectHttpsLayer::default());§Custom Configuration
use axum::{routing::get, Router};
use http::StatusCode;
use rune_axum_redirect_https::{RedirectHttps, RedirectHttpsLayer};
// HTTP on :8080 → HTTPS on :8443, using 301 for legacy clients
let layer = RedirectHttpsLayer::new(
RedirectHttps::new()
.status(StatusCode::MOVED_PERMANENTLY)
.https_port(8443),
);
let app: Router = Router::new()
.route("/", get(|| async { "hello" }))
.layer(layer);Structs§
- Redirect
Https - Configuration for the HTTP-to-HTTPS redirect middleware.
- Redirect
Https Layer - Tower
Layerthat redirects HTTP requests to HTTPS.