Traefik & File

Good Old Configuration File

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

Info

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 & Services

Enabling the file provider:

File (TOML)

  1. [providers.file]
  2. directory = "/path/to/dynamic/conf"

File (YAML)

  1. providers:
  2. file:
  3. directory: "/path/to/dynamic/conf"

CLI

  1. --providers.file.directory=/path/to/dynamic/conf

Declaring Routers, Middlewares & Services:

TOML

  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/"

YAML

  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

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

Limitations

With the file provider, Traefik listens for file system notifications to update the dynamic configuration.

If you use a mounted/bound file system in your orchestrator (like docker or kubernetes), the way the files are linked may be a source of errors. If the link between the file systems is broken, when a source file/directory is changed/renamed, nothing will be reported to the linked file/directory, so the file system notifications will be neither triggered nor caught.

For example, in docker, if the host file is renamed, the link to the mounted file will be broken and the container’s file will not be updated. To avoid this kind of issue, a good practice is to:

  • set the Traefik directory configuration with the parent directory
  • mount/bind the parent directory

As it is very difficult to listen to all file system notifications, Traefik use fsnotify. If using a directory with a mounted directory does not fix your issue, please check your file system compatibility with fsnotify.

filename

Defines the path to the configuration file.

filename and directory are mutually exclusive. The recommendation is to use directory.

File (TOML)

  1. [providers]
  2. [providers.file]
  3. filename = "/path/to/config/dynamic_conf.toml"

File (YAML)

  1. providers:
  2. file:
  3. filename: /path/to/config/dynamic_conf.yml

CLI

  1. --providers.file.filename=/path/to/config/dynamic_conf.toml

directory

Defines the path to the directory that contains the configuration files.

filename and directory are mutually exclusive. The recommendation is to use directory.

File (TOML)

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

File (YAML)

  1. providers:
  2. file:
  3. directory: /path/to/config

CLI

  1. --providers.file.directory=/path/to/config

watch

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.

File (TOML)

  1. [providers]
  2. [providers.file]
  3. directory = "/path/to/dynamic/conf"
  4. watch = true

File (YAML)

  1. providers:
  2. file:
  3. directory: /path/to/dynamic/conf
  4. watch: true

CLI

  1. --providers.file.directory=/my/path/to/dynamic/conf
  2. --providers.file.watch=true

Go Templating

Warning

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

Traefik supports using Go templating to automatically generate repetitive portions of configuration files. These sections must be valid Go templates, augmented with the Sprig template functions.

To illustrate, it’s possible to easily define multiple routers, services, and TLS certificates as described in the following examples:

Configuring Using Templating

TOML

  1. # template-rules.toml
  2. [http]
  3. [http.routers]
  4. {{ range $i, $e := until 100 }}
  5. [http.routers.router{{ $e }}-{{ env "MY_ENV_VAR" }}]
  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. stores = ["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 }}

YAML

  1. http:
  2. routers:
  3. {{range $i, $e := until 100 }}
  4. router{{ $e }}-{{ env "MY_ENV_VAR" }}:
  5. # ...
  6. {{end}}
  7. services:
  8. {{range $i, $e := until 100 }}
  9. application{{ $e }}:
  10. # ...
  11. {{end}}
  12. tcp:
  13. routers:
  14. {{range $i, $e := until 100 }}
  15. router{{ $e }}:
  16. # ...
  17. {{end}}
  18. services:
  19. {{range $i, $e := until 100 }}
  20. service{{ $e }}:
  21. # ...
  22. {{end}}
  23. tls:
  24. certificates:
  25. {{ range $i, $e := until 10 }}
  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}}