Traefik & 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, …

    Declaring Routers, Middlewares & Services

    Enabling the file provider:

    1. file:
    2. directory: "/path/to/dynamic/conf"
    1. --providers.file.directory=/path/to/dynamic/conf
    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. - my-basic-auth
    8. service: service-foo
    9. rule: Path(`/foo`)
    10. # Add the middleware
    11. middlewares:
    12. my-basic-auth:
    13. basicAuth:
    14. - test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/
    15. - test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0
    16. usersFile: etc/traefik/.htpasswd
    17. # Add the service
    18. services:
    19. service-foo:
    20. loadBalancer:
    21. servers:
    22. - url: http://foo/
    23. - url: http://bar/
    24. passHostHeader: false

    Provider Configuration

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

    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 . If using a directory with a mounted directory does not fix your issue, please check your file system compatibility with fsnotify.

    Defines the path to the configuration file.

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

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

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

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

    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. directory = "/path/to/dynamic/conf"
    4. watch = true
    1. providers:
    2. file:
    3. directory: /path/to/dynamic/conf
    4. watch: true
    1. --providers.file.directory=/my/path/to/dynamic/conf

    Warning

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

    Traefik allows using Go templating, it must be a valid Go template, augmented with the .

    Configuring Using Templating

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