Running with Hyper
Axum uses Hyper under the hood. The standard way to run an Axum app:Binding to
0.0.0.0 makes your server accessible from outside the host machine. Use 127.0.0.1 for local-only access.Graceful shutdown
Handle shutdown signals gracefully to avoid dropping in-flight requests:Basic graceful shutdown
1
Handle SIGTERM and SIGINT
Listen for shutdown signals from the operating system or container orchestrator.
2
Stop accepting new requests
The server stops accepting new connections but continues processing existing ones.
3
Wait for in-flight requests
Allow existing requests to complete normally.
4
Clean shutdown
Close all connections and exit gracefully.
With timeout protection
Prevent requests from hanging during shutdown:Advanced serving with Hyper
For more control over the HTTP server, use Hyper’s low-level API:When to use low-level Hyper API
When to use low-level Hyper API
- Custom connection handling logic
- Per-connection state or middleware
- Fine-grained control over HTTP/1.1 vs HTTP/2
- Custom TLS configuration
- Performance optimization for specific use cases
Configuration management
Use environment variables and configuration files:Logging and observability
Implement comprehensive logging for production:Performance optimization
Connection pooling
Use connection pools for databases:Response compression
Body size limits
Docker deployment
Create an optimized Dockerfile:Multi-stage builds keep your final image small by excluding build dependencies.