
Service trait
Learn what the Tower Service trait is, why it sits underneath every Axum handler and middleware, and how to implement it yourself with working examples
Layer trait in Axum middleware
Learn what the Tower Layer trait is, how it builds Services, and how to write configurable middleware layers in Axum with working examples
Cross-cutting concerns in Axum middleware
Learn what cross-cutting concerns are, why they belong in middleware instead of handlers, and how to apply them in Axum with working examples
axum middleware module
Learn what the axum middleware module provides, how from_fn and Next work, and how to choose the right middleware approach for your application
tower-http crate
Learn what the tower-http crate is, why Axum uses it, how to add it to your Rust project, and how to apply its prebuilt middleware layers to your Axum routes.
CorsLayer
Learn how to configure Cross-Origin Resource Sharing (CORS) in Axum using tower-http CorsLayer, with step-by-step examples and common pitfalls.
Tracing HTTP Requests with TraceLayer
Learn how to use the tower-http TraceLayer middleware to automatically attach request context to logs in Axum applications, customize span fields, and handle request logging with practical examples.
CompressionLayer for Automatic Response Compression in Axum
Learn how to compress HTTP response bodies in Axum using tower-http CompressionLayer, including gzip, brotli, deflate, and zstd configuration
TimeoutLayer in Axum
Learn how to set time limits on HTTP requests using TimeoutLayer in Axum, handle timeout errors gracefully, and apply different timeouts per route.
RequestBodyLimitLayer - Limiting Request Body Size in Axum
Learn how RequestBodyLimitLayer caps request body size in Axum, how it works with the built in 2 MB default limit, and how to set different limits per route
SetSensitiveHeadersLayer - Keeping Secrets Out of Logs in Axum
Learn how SetSensitiveHeadersLayer marks request and response headers as sensitive so logging middleware redacts them instead of printing secrets
PropagateHeaderLayer - Copying Request Headers Into Responses in Axum
Learn how PropagateHeaderLayer copies a header from an incoming request into the outgoing response, how to propagate several headers, and how layer order changes what other middleware sees
ServeDir - Serving Static Files From a Directory in Axum
Learn how ServeDir serves files from a folder in Axum, how to mount it at the root or under a path, how to fall back to index.html for single-page apps, and how precompressed files work
Middleware Layer Ordering in Axum With tower-http
Understand how middleware layers run in Axum, why repeated layer calls and ServiceBuilder give opposite orders, and where to place TraceLayer, CompressionLayer, TimeoutLayer, and SetSensitiveHeadersLayer
middleware from_fn - Writing Custom Middleware in Axum
Learn how to write custom middleware in Axum with middleware from_fn, from logging and timing to authentication, short-circuiting, and Result returns
fromfnwith_state - Middleware With Application State in Axum
Learn how to write Axum middleware that reads shared application state with from_fn_with_state, including counters, configuration, and authentication
from_extractor - Turning an Extractor Into Middleware in Axum
Learn how axum middleware from_extractor runs an extractor as a validation gate before handlers, why the value is discarded, and how to pass data through extensions
fromextractorwith_state - Stateful Extractor Middleware in Axum
Learn how axum middleware from_extractor_with_state runs an extractor with application state before handlers, with API key checks, user lookups, and custom rejections
map_request - Changing Requests Before Handlers in Axum
Learn how axum middleware map_request transforms incoming requests with an async function, including headers, extractors, rejection, extensions, and URI rewrites
maprequestwith_state in Axum
Learn how to use axum middleware map_request_with_state to change incoming requests using shared application state, with complete beginner examples and explanations
map_response in Axum Middleware
Learn how to use axum middleware map_response to change HTTP responses after handlers run, with ordering rules, extractors, body handling, and complete beginner examples
mapresponsewith_state in Axum
Learn how to write Axum middleware that changes outgoing responses while reading shared application state, with complete beginner examples and explanations
The Next Type in Axum
Learn what axum middleware Next is, how next run passes control to the rest of the middleware stack, and how to short-circuit or post-process responses with it
Request Logging Middleware in Axum
Learn how to log HTTP requests in Axum with tower-http TraceLayer and custom from_fn logging middleware, with complete beginner examples and explanations
Auth Check Middleware Preview
Learn how to write a simple authentication check middleware in Axum that reads a bearer token, rejects bad requests, and passes the logged-in user to handlers
The layer Method in Axum
How the layer method attaches Tower middleware to a Router, a MethodRouter, or a single handler, how ordering and scope work, and how to handle middleware that can fail
route_layer in Axum
How route_layer attaches middleware only to routes that match, why 404 and 405 responses stay intact, and how it differs from layer and the method-router forms
Global Middleware in Axum
What global middleware means in Axum, the two rules that govern it, and how it differs from middleware that runs only when a route matches
Per-Route Middleware in Axum
How to organise per-route middleware across a real router with sub-routers per concern, merged groups, and a coverage test that proves every route carries the middleware it should
Middleware Ordering Rules in Axum
How middleware order works across router, route, method and handler scopes in Axum, including merge, nest, short circuits and error handler placement
tower ServiceBuilder in Axum
How to compose middleware with tower ServiceBuilder in Axum, including its methods, feature flags, tower-http one-liners and the two compile bounds a builder must satisfy
RequestId Correlation Layer in Axum
Learn how to give every request a correlation id with tower-http SetRequestIdLayer and PropagateRequestIdLayer, read it in a handler, generate custom ids, and avoid the ordering trap that silently drops generated ids
SetRequestHeader Layer in Axum
Learn tower-http SetRequestHeaderLayer in axum, its three modes overriding appending and if_not_present, fixed values versus closures computed from the request, and its response-side twin
RateLimitLayer Overview in Axum
Learn why tower RateLimitLayer fails in axum, first refusing to compile and then silently doing nothing on normal routes, and the hand-rolled Arc-state limiter that returns 429 and actually works
Manual Service Implementation for Middleware in Axum
Learn how to write a tower Service by hand as middleware - short-circuiting, response inspection, readiness gating, and the exact bounds axum requires
Manual Layer Implementation for Middleware in Axum
Learn the Layer lifecycle in axum 0.8 - when layer runs, what may live in the layer struct, why shared state must sit behind an Arc, and when layer_fn replaces a struct
Injecting Extension from Middleware in Axum
Learn the complete Extension system in axum - the three ways middleware injects values into request extensions, Extension compared with State, type-keying rules, and the response direction