API
As with all features of Traefik, this handler can be enabled with the static configuration.
Enabling the API in production is not recommended, because it will expose all configuration elements, including sensitive data.
In production, it should be at least secured by authentication and authorizations.
A good sane default (non exhaustive) set of recommendations would be to apply the following protection mechanisms:
- At the transport level: NOT publicly exposing the API’s port, keeping it restricted to internal networks (as in the , applied to networks).
If you enable the API, a new special service
named api@internal
is created and can then be referenced in a router.
To enable the API handler, use the following option on the static configuration:
File (TOML)
File (YAML)
# Static Configuration
api: {}
CLI
--api=true
And then define a routing configuration on Traefik itself with the :
Docker
# Dynamic Configuration
labels:
- "traefik.http.routers.api.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.api.service=api@internal"
- "traefik.http.routers.api.middlewares=auth"
- "traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"
Docker (Swarm)
# Dynamic Configuration
deploy:
labels:
- "traefik.http.routers.api.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.api.service=api@internal"
- "traefik.http.routers.api.middlewares=auth"
- "traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"
# Dummy service for Swarm port detection. The port can be any valid integer value.
- "traefik.http.services.dummy-svc.loadbalancer.server.port=9999"
Kubernetes CRD
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: traefik-dashboard
spec:
routes:
- match: Host(`traefik.example.com`)
kind: Rule
- name: api@internal
kind: TraefikService
middlewares:
- name: auth
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: auth
spec:
basicAuth:
secret: secretName # Kubernetes secret named "secretName"
# Dynamic Configuration
- "traefik.http.routers.api.service=api@internal"
- "traefik.http.routers.api.middlewares=auth"
- "traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"
Marathon
Rancher
# Dynamic Configuration
labels:
- "traefik.http.routers.api.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.api.service=api@internal"
- "traefik.http.routers.api.middlewares=auth"
- "traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"
File (TOML)
# Dynamic Configuration
[http.routers.my-api]
rule = "Host(`traefik.example.com`)"
service = "api@internal"
middlewares = ["auth"]
[http.middlewares.auth.basicAuth]
users = [
"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
File (YAML)
# Dynamic Configuration
http:
routers:
api:
rule: Host(`traefik.example.com`)
service: api@internal
middlewares:
- auth
middlewares:
auth:
basicAuth:
users:
- "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"
- "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"
The router’s rule must catch requests for the URI path /api
Using an “Host” rule is recommended, by catching all the incoming traffic on this host domain to the API. However, you can also use “path prefix” rule or any combination or rules.
Host Rule
# Matches http://traefik.example.com, http://traefik.example.com/api
# or http://traefik.example.com/hello
rule = "Host(`traefik.example.com`)"
Path Prefix Rule
# Matches http://api.traefik.example.com/api or http://example.com/api
# but does not match http://api.traefik.example.com/hello
rule = "PathPrefix(`/api`)"
Combination of Rules
# Matches http://traefik.example.com/api or http://traefik.example.com/dashboard
# but does not match http://traefik.example.com/hello
rule = "Host(`traefik.example.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))"
Enable the API in insecure
mode, which means that the API will be available directly on the entryPoint named traefik
.
Info
If the entryPoint named traefik
is not configured, it will be automatically created on port 8080.
File (TOML)
api:
insecure: true
CLI
--api.insecure=true
Optional, Default=true
Enable the dashboard. More about the dashboard features .
File (TOML)
[api]
dashboard = true
File (YAML)
api:
dashboard: true
CLI
--api.dashboard=true
With Dashboard enabled, the router rule must catch requests for both /api
and /dashboard
Please check the to learn more about this and to get examples.
Optional, Default=false
Enable additional endpoints for debugging and profiling, served under /debug/
.
File (TOML)
[api]
debug = true
File (YAML)
CLI