Routing Overview
Understand how Traefik routes requests through its architecture.How Routing Works
Traefik’s routing architecture connects incoming requests to backend services through a sophisticated pipeline:Routing Flow
- EntryPoints listen for incoming traffic on specific ports (e.g.,
:80,:443) - Routers analyze requests using rules to match patterns (host, path, headers)
- Middleware transforms requests (authentication, rate limiting, headers)
- Services load balance and forward requests to backend servers
Every request must match a router rule to be forwarded to a service. Unmatched requests return a 404 error.
Component Responsibilities
EntryPoints
Network entry points that listen on specific ports for TCP or UDP traffic
Routers
Analyze incoming requests and match them against rules (Host, Path, Headers)
Services
Load balance requests across multiple backend servers with health checks
Middleware
Transform requests and responses (auth, rate limiting, compression)
Complete Routing Example
Here’s a complete example showing all routing components working together:What This Configuration Does
- EntryPoints: Listens on ports 80 (HTTP) and 443 (HTTPS)
- Router: Matches requests to
api.example.comwith path/v1/* - Middleware: Applies basic authentication and rate limiting
- Service: Load balances between two backend servers with health checks
- TLS: Automatically obtains certificates from Let’s Encrypt
Protocol Support
Traefik supports multiple protocols for routing:- HTTP/HTTPS
- TCP
- UDP
Full HTTP/1.1, HTTP/2, and HTTP/3 support with rich routing rules:
- Host matching:
Host(example.com) - Path matching:
PathPrefix(/api) - Header matching:
Header(Content-Type,application/json) - Method matching:
Method(POST) - Query parameters:
Query(mobile,true)
Router Priority
When multiple routers match a request, Traefik uses priority to determine which router handles the request.Dynamic Configuration
Traefik supports dynamic configuration from multiple providers:- File - YAML/TOML configuration files
- Docker - Container labels
- Kubernetes - Ingress, IngressRoute CRDs
- Consul/Etcd - Key-value stores
- HTTP - REST API endpoint
Best Practices
Use explicit priorities for overlapping routes
Use explicit priorities for overlapping routes
When you have multiple routers that could match the same request, set explicit priorities to ensure predictable routing:
Combine rules with logical operators
Combine rules with logical operators
Use
&& (AND), || (OR), and ! (NOT) for complex routing:Enable health checks for production services
Enable health checks for production services
Always configure health checks to automatically remove unhealthy servers:
Use middleware chains efficiently
Use middleware chains efficiently
Order middleware carefully - authentication should come before rate limiting:
Next Steps
Configure EntryPoints
Set up network entry points for HTTP, HTTPS, TCP, and UDP
Create Routers
Define routing rules to match and forward requests
Setup Services
Configure load balancing and health checks for backends