Traefik & File

Good Old Configuration File

The file provider lets you define the dynamic configuration in a YAML or TOML file.

It supports providing configuration through a single configuration file or multiple separate files.

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 solution for reusing common elements from other providers (e.g. declaring whitelist middlewares, basic authentication, …)

Configuration Examples

Declaring Routers, Middlewares & Services

Enabling the file provider:

File (YAML)

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

File (TOML)

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

CLI

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

Declaring Routers, Middlewares & Services:

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

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

Provider Configuration

For an overview of all the options that can be set with the file provider, see the dynamic configuration and static configuration references.

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 is broken and the container’s file is no longer updated. To avoid this kind of issue, it is recommended 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 uses 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.

The filename and directory options are mutually exclusive. It is recommended to use directory.

File (YAML)

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

File (TOML)

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

CLI

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

directory

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

The filename and directory options are mutually exclusive. It is recommended to use directory.

File (YAML)

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

File (TOML)

  1. [providers]
  2. [providers.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 (YAML)

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

File (TOML)

  1. [providers]
  2. [providers.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 sections of configuration files. These sections must be a valid Go template, and can use sprig template functions.

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

Configuring Using Templating

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}}

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 }}

Using Traefik for Business Applications?

If you are using Traefik for commercial applications, consider the Enterprise Edition. You can use it as your:

Traefik Enterprise enables centralized access management, distributed Let’s Encrypt, and other advanced capabilities. Learn more in this 15-minute technical walkthrough.