Traefik 101: Modern Reverse Proxy with Auto-Discovery
A practical introduction to Traefik for developers familiar with nginx. Learn core concepts, setup examples, and when to choose Traefik over traditional reverse proxies.
Abstract
Traefik is a modern reverse proxy and load balancer built for dynamic infrastructure. Unlike traditional reverse proxies that require manual configuration updates, Traefik automatically discovers services and updates routing rules. This introduction covers core concepts, practical Docker setups, and honest comparisons with nginx to help you choose the right tool for your use case.
What is Traefik?
Traefik is a cloud-native edge router that automatically discovers services in your infrastructure and configures itself accordingly. While nginx requires you to manually edit configuration files and reload the service when your infrastructure changes, Traefik watches your container orchestrator (Docker, Kubernetes) and updates routing rules automatically.
I've learned that Traefik shines in environments where services come and go frequently - think microservices deployments, development environments, or platforms where developers deploy their own services. It's not trying to replace nginx everywhere; it's solving a specific pain point: dynamic configuration management.
The Core Problem Traefik Solves
Here's a scenario I've encountered repeatedly: You deploy a new microservice to your Docker Swarm or Kubernetes cluster. With nginx, you need to:
- Edit the nginx configuration file
- Test the configuration
- Reload nginx
- Hope nothing breaks
With Traefik, you add labels to your Docker container or annotations to your Kubernetes service, and routing rules are updated automatically. No SSH into servers, no configuration file edits, no service reloads.
Core Concepts
1. Providers
Providers are how Traefik discovers services. Common providers include:
- Docker: Watches Docker containers and their labels
- Kubernetes: Reads Ingress/IngressRoute resources
- File: Traditional configuration files (yes, you can still use static config)
- Consul, Etcd: Service discovery backends
Working with Docker taught me that the provider abstraction is powerful - you can start with Docker labels in development and move to Kubernetes annotations in production using the same mental model.
2. Entrypoints
Entrypoints define the ports Traefik listens on. Typical setup:
3. Routers
Routers connect entrypoints to services based on rules. Rules can match:
- Host headers:
Host(api.example.com) - Path prefixes:
PathPrefix(/api) - Headers:
Headers(X-Custom-Header,value) - Multiple conditions:
Host(api.example.com) && PathPrefix(/v1)
4. Middlewares
Middlewares transform requests/responses. This is where Traefik gets powerful:
- Redirect HTTP to HTTPS
- Add/remove headers
- Rate limiting
- Basic authentication
- Circuit breakers
- Compression
Practical Setup Example
Let's build a real working example. Here's a Docker Compose setup with Traefik managing two services:
Start this with docker-compose up -d and you can access:
http://whoami.localhost- The whoami servicehttp://api.localhost- The nginx servicehttp://traefik.localhost- Traefik dashboard
The Equivalent nginx Configuration
For comparison, here's what you'd need with nginx for the same setup:
The nginx config is perfectly fine, but notice what happens when you add a new service: you edit this file, test it, and reload nginx. With Traefik, you just start a new container with labels.
Middleware in Action
Here's a practical example using middlewares for HTTPS redirect and rate limiting:
Implementing the same rate limiting in nginx requires either ngx_http_limit_req_module (which works well) or external tools. Both approaches work; Traefik's version is just configured differently.
Request Flow Architecture
Here's how requests flow through Traefik compared to a traditional setup:
When to Choose Traefik Over nginx
I've learned that the choice isn't about "better" but about fit for purpose. Here's what works:
Choose Traefik When:
- Dynamic infrastructure: Services are deployed/removed frequently
- Container-native: Running Docker, Kubernetes, or similar orchestrators
- Developer self-service: Teams deploy their own services with labels/annotations
- Automatic HTTPS: Let's Encrypt integration is built-in and trivial to set up
- Microservices: Many small services that change independently
Real scenario: Working with a platform team that supported 30+ development teams deploying 200+ services. Traefik's auto-discovery reduced routing configuration overhead by about 70% compared to our previous nginx setup. No more tickets to update reverse proxy configs.
Choose nginx When:
- Static infrastructure: Services rarely change
- Complex URL rewriting: nginx's rewrite engine is more mature
- Maximum performance: nginx has lower latency (though the difference is small)
- Advanced caching: nginx's caching capabilities are more sophisticated
- Serving static files: nginx excels at this
Real scenario: For a high-traffic content delivery setup serving primarily static assets, nginx with proper caching configuration outperformed our Traefik setup. The configuration complexity was worth it for the performance gain.
Common Patterns and Gotchas
Pattern 1: Multiple Routers per Service
You can route different paths to the same service with different middleware:
Pattern 2: Weighted Load Balancing
Gotcha 1: Docker Socket Access
Traefik needs read access to the Docker socket. In production, consider using a Docker socket proxy like tecnativa/docker-socket-proxy to limit exposure.
Gotcha 2: Label Syntax
Labels are strings, so complex values need proper escaping:
Gotcha 3: Default Settings
By default, Traefik exposes ALL containers. Set exposedbydefault=false and explicitly enable services:
Real-World Use Cases
Use Case 1: Development Environment
Here's what works for multi-service local development:
Add 127.0.0.1 app.local to /etc/hosts, and you have a unified local development URL.
Use Case 2: Automatic HTTPS with Let's Encrypt
Traefik handles certificate acquisition, renewal, and serving automatically. With nginx, you'd typically use certbot with cron jobs and hooks to reload nginx.
Performance Considerations
In my experience, both Traefik and nginx perform excellently for most workloads. Here's what I've observed:
- Latency: nginx typically adds 1-2ms, Traefik adds 2-4ms (imperceptible for most applications)
- Throughput: nginx handles slightly higher requests per second in synthetic benchmarks
- Resource usage: Traefik uses more memory (50-100MB baseline vs nginx's 10-20MB)
- CPU: Comparable under normal loads; nginx pulls ahead under extreme traffic
For most applications, the operational benefits of Traefik's auto-discovery outweigh the small performance difference. For ultra-high-traffic static content delivery, nginx's performance advantage matters more.
Migration Strategy
If you're considering moving from nginx to Traefik, here's what works:
- Start hybrid: Run both nginx and Traefik side-by-side
- Migrate incrementally: Move services one at a time
- Use file provider: Traefik can read static config files (similar to nginx)
- Test thoroughly: Especially middleware chains and routing rules
- Monitor metrics: Compare latency, error rates, resource usage
I've learned that you don't need to migrate everything. Some teams run nginx for static content and APIs, while Traefik handles dynamic microservices.
Conclusion
Traefik isn't replacing nginx - it's solving a different problem. If you're running dynamic infrastructure with containers, Traefik's auto-discovery can significantly reduce operational overhead. If you're serving static content or running stable infrastructure, nginx's maturity and performance remain compelling.
Here's what works for choosing between them:
- Dynamic microservices, frequent deployments → Traefik
- Static infrastructure, maximum performance → nginx
- Need both? → Run them together
The best part is that both are excellent tools, and you can use whichever fits your current needs. Start with a small experiment using the Docker Compose examples above, and see which mental model fits your workflow better.
Further Resources
- Traefik Documentation
- Traefik Middleware Reference
- Docker Provider Documentation
- Let's Encrypt with Traefik
The configuration examples in this post are available to copy and try in your own environment. Start with the basic setup, then explore middlewares and automatic HTTPS based on your needs.