• Traefik & File
    • Configuration Examples
    • Provider Configuration Options
      • filename
      • directory
      • watch
      • Go Templating

    Traefik & File

    Good Old Configuration File

    The file provider lets you define the dynamic configuration in a TOML or YAML file.You can write these configuration elements:

    • In a dedicated file
    • In several dedicated files

    Note

    The file provider is the default format used throughout the documentation to show samples of the configuration for many features.

    Tip

    The file provider can be a good location for common elements you'd like to re-use from other providers; e.g. declaring whitelist middlewares, basic authentication, …

    Configuration Examples

    Declaring Routers, Middlewares & ServicesEnabling the file provider:

    1. [providers.file]
    2. filename = "/my/path/to/dynamic-conf.toml"
    1. providers:
    2. file:
    3. filename: "/my/path/to/dynamic-conf.yml"
    1. --providers.file.filename=/my/path/to/dynamic_conf.toml

    Declaring Routers, Middlewares & Services:

    1. [http]
    2. # Add the router
    3. [http.routers]
    4. [http.routers.router0]
    5. entryPoints = ["web"]
    6. middlewares = ["my-basic-auth"]
    7. service = "service-foo"
    8. rule = "Path(`/foo`)"
    9. # Add the middleware
    10. [http.middlewares]
    11. [http.middlewares.my-basic-auth.basicAuth]
    12. users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
    13. "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"]
    14. usersFile = "etc/traefik/.htpasswd"
    15. # Add the service
    16. [http.services]
    17. [http.services.service-foo]
    18. [http.services.service-foo.loadBalancer]
    19. [[http.services.service-foo.loadBalancer.servers]]
    20. url = "http://foo/"
    21. [[http.services.service-foo.loadBalancer.servers]]
    22. url = "http://bar/"
    1. http:
    2. # Add the router
    3. routers:
    4. router0:
    5. entryPoints:
    6. - web
    7. middlewares:
    8. - my-basic-auth
    9. service: service-foo
    10. rule: Path(`/foo`)
    11. # Add the middleware
    12. middlewares:
    13. my-basic-auth:
    14. basicAuth:
    15. users:
    16. - test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/
    17. - test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0
    18. usersFile: etc/traefik/.htpasswd
    19. # Add the service
    20. services:
    21. service-foo:
    22. loadBalancer:
    23. servers:
    24. - url: http://foo/
    25. - url: http://bar/
    26. passHostHeader: false

    Provider Configuration Options

    Browse the Reference

    If you're in a hurry, maybe you'd rather go through the static and the dynamic configuration references.

    filename

    Optional

    Defines the path of the configuration file.

    1. [providers]
    2. [providers.file]
    3. filename = "dynamic_conf.toml"
    1. providers:
    2. file:
    3. filename: dynamic_conf.yml
    1. --providers.file.filename=dynamic_conf.toml

    directory

    Optional

    Defines the directory that contains the configuration files.

    1. [providers]
    2. [providers.file]
    3. directory = "/path/to/config"
    1. providers:
    2. file:
    3. directory: /path/to/config
    1. --providers.file.directory=/path/to/config

    watch

    Optional

    Set the watch option to true to allow Traefik to automatically watch for file changes.It works with both the filename and the directory options.

    1. [providers]
    2. [providers.file]
    3. filename = "dynamic_conf.toml"
    4. watch = true
    1. providers:
    2. file:
    3. filename: dynamic_conf.yml
    4. watch: true
    1. --providers.file.filename=dynamic_conf.toml
    2. --providers.file.watch=true

    Go Templating

    Warning

    Go Templating only works along with dedicated configuration files.Templating does not work in the Traefik main configuration file.

    Traefik allows using Go templating.Thus, it's possible to define easily lot of routers, services and TLS certificates as described in the file template-rules.toml :Configuring Using Templating

    1. # template-rules.toml
    2. [http]
    3. [http.routers]
    4. {{ range $i, $e := until 100 }}
    5. [http.routers.router{{ $e }}]
    6. # ...
    7. {{ end }}
    8. [http.services]
    9. {{ range $i, $e := until 100 }}
    10. [http.services.service{{ $e }}]
    11. # ...
    12. {{ end }}
    13. [tcp]
    14. [tcp.routers]
    15. {{ range $i, $e := until 100 }}
    16. [tcp.routers.router{{ $e }}]
    17. # ...
    18. {{ end }}
    19. [tcp.services]
    20. {{ range $i, $e := until 100 }}
    21. [http.services.service{{ $e }}]
    22. # ...
    23. {{ end }}
    24. {{ range $i, $e := until 10 }}
    25. [[tls.certificates]]
    26. certFile = "/etc/traefik/cert-{{ $e }}.pem"
    27. keyFile = "/etc/traefik/cert-{{ $e }}.key"
    28. store = ["my-store-foo-{{ $e }}", "my-store-bar-{{ $e }}"]
    29. {{ end }}
    30. [tls.config]
    31. {{ range $i, $e := until 10 }}
    32. [tls.config.TLS{{ $e }}]
    33. # ...
    34. {{ end }}
    1. http:
    2. {{range $i, $e := until 100 }}
    3. routers:
    4. router{{ $e }:
    5. # ...
    6. {{end}}
    7. {{range $i, $e := until 100 }}
    8. services:
    9. application{{ $e }}:
    10. # ...
    11. {{end}}
    12. tcp:
    13. {{range $i, $e := until 100 }}
    14. routers:
    15. router{{ $e }:
    16. # ...
    17. {{end}}
    18. {{range $i, $e := until 100 }}
    19. services:
    20. service{{ $e }}:
    21. # ...
    22. {{end}}
    23. {{ range $i, $e := until 10 }}
    24. tls:
    25. certificates:
    26. - certFile: "/etc/traefik/cert-{{ $e }}.pem"
    27. keyFile: "/etc/traefik/cert-{{ $e }}.key"
    28. store:
    29. - "my-store-foo-{{ $e }}"
    30. - "my-store-bar-{{ $e }}"
    31. {{end}}