File Provider

Traefik can be configured with a file.

Reference

  1. [file]
  2. # Backends
  3. [backends]
  4. [backends.backend1]
  5. [backends.backend1.servers]
  6. [backends.backend1.servers.server0]
  7. url = "http://10.10.10.1:80"
  8. weight = 1
  9. [backends.backend1.servers.server1]
  10. url = "http://10.10.10.2:80"
  11. weight = 2
  12. # ...
  13. [backends.backend1.circuitBreaker]
  14. expression = "NetworkErrorRatio() > 0.5"
  15. [backends.backend1.responseForwarding]
  16. flushInterval = "10ms"
  17. [backends.backend1.loadBalancer]
  18. method = "drr"
  19. [backends.backend1.loadBalancer.stickiness]
  20. cookieName = "foobar"
  21. secure = true
  22. httpOnly = true
  23. sameSite = "foobar"
  24. [backends.backend1.maxConn]
  25. amount = 10
  26. extractorfunc = "request.host"
  27. [backends.backend1.healthCheck]
  28. path = "/health"
  29. port = 88
  30. interval = "30s"
  31. scheme = "http"
  32. hostname = "myhost.com"
  33. [backends.backend1.healthcheck.headers]
  34. My-Custom-Header = "foo"
  35. My-Header = "bar"
  36. [backends.backend2]
  37. # ...
  38. # Frontends
  39. [frontends]
  40. [frontends.frontend1]
  41. entryPoints = ["http", "https"]
  42. backend = "backend1"
  43. passHostHeader = true
  44. priority = 42
  45. # Use frontends.frontend1.auth.basic below instead
  46. basicAuth = [
  47. "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
  48. "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
  49. ]
  50. [frontends.frontend1.passTLSClientCert]
  51. pem = true
  52. [frontends.frontend1.passTLSClientCert.infos]
  53. notBefore = true
  54. notAfter = true
  55. [frontends.frontend1.passTLSClientCert.infos.subject]
  56. country = true
  57. domainComponent = true
  58. province = true
  59. locality = true
  60. organization = true
  61. commonName = true
  62. serialNumber = true
  63. [frontends.frontend1.passTLSClientCert.infos.issuer]
  64. country = true
  65. domainComponent = true
  66. province = true
  67. locality = true
  68. organization = true
  69. commonName = true
  70. serialNumber = true
  71. [frontends.frontend1.auth]
  72. headerField = "X-WebAuth-User"
  73. [frontends.frontend1.auth.basic]
  74. removeHeader = true
  75. users = [
  76. "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
  77. "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
  78. ]
  79. usersFile = "/path/to/.htpasswd"
  80. [frontends.frontend1.auth.digest]
  81. removeHeader = true
  82. users = [
  83. "test:traefik:a2688e031edb4be6a3797f3882655c05",
  84. "test2:traefik:518845800f9e2bfb1f1f740ec24f074e",
  85. ]
  86. usersFile = "/path/to/.htdigest"
  87. [frontends.frontend1.auth.forward]
  88. address = "https://authserver.com/auth"
  89. trustForwardHeader = true
  90. authResponseHeaders = ["X-Auth-User"]
  91. [frontends.frontend1.auth.forward.tls]
  92. ca = "path/to/local.crt"
  93. caOptional = true
  94. cert = "path/to/foo.cert"
  95. key = "path/to/foo.key"
  96. insecureSkipVerify = true
  97. [frontends.frontend1.whiteList]
  98. sourceRange = ["10.42.0.0/16", "152.89.1.33/32", "afed:be44::/16"]
  99. useXForwardedFor = true
  100. [frontends.frontend1.routes]
  101. [frontends.frontend1.routes.route0]
  102. rule = "Host:test.localhost"
  103. [frontends.frontend1.routes.Route1]
  104. rule = "Method:GET"
  105. # ...
  106. [frontends.frontend1.headers]
  107. allowedHosts = ["foobar", "foobar"]
  108. hostsProxyHeaders = ["foobar", "foobar"]
  109. SSLRedirect = true
  110. SSLTemporaryRedirect = true
  111. SSLHost = "foobar"
  112. STSSeconds = 42
  113. STSIncludeSubdomains = true
  114. STSPreload = true
  115. forceSTSHeader = true
  116. frameDeny = true
  117. customFrameOptionsValue = "foobar"
  118. contentTypeNosniff = true
  119. browserXSSFilter = true
  120. contentSecurityPolicy = "foobar"
  121. publicKey = "foobar"
  122. referrerPolicy = "foobar"
  123. isDevelopment = true
  124. [frontends.frontend1.headers.customRequestHeaders]
  125. X-Foo-Bar-01 = "foobar"
  126. X-Foo-Bar-02 = "foobar"
  127. # ...
  128. [frontends.frontend1.headers.customResponseHeaders]
  129. X-Foo-Bar-03 = "foobar"
  130. X-Foo-Bar-04 = "foobar"
  131. # ...
  132. [frontends.frontend1.headers.SSLProxyHeaders]
  133. X-Foo-Bar-05 = "foobar"
  134. X-Foo-Bar-06 = "foobar"
  135. # ...
  136. [frontends.frontend1.errors]
  137. [frontends.frontend1.errors.errorPage0]
  138. status = ["500-599"]
  139. backend = "error"
  140. query = "/{status}.html"
  141. [frontends.frontend1.errors.errorPage1]
  142. status = ["404", "403"]
  143. backend = "error"
  144. query = "/{status}.html"
  145. # ...
  146. [frontends.frontend1.ratelimit]
  147. extractorfunc = "client.ip"
  148. [frontends.frontend1.ratelimit.rateset.rateset1]
  149. period = "10s"
  150. average = 100
  151. burst = 200
  152. [frontends.frontend1.ratelimit.rateset.rateset2]
  153. period = "3s"
  154. average = 5
  155. burst = 10
  156. # ...
  157. [frontends.frontend1.redirect]
  158. entryPoint = "https"
  159. regex = "^http://localhost/(.*)"
  160. replacement = "http://mydomain/$1"
  161. permanent = true
  162. [frontends.frontend2]
  163. # ...
  164. # HTTPS certificates
  165. [[tls]]
  166. entryPoints = ["https"]
  167. [tls.certificate]
  168. certFile = "path/to/my.cert"
  169. keyFile = "path/to/my.key"
  170. [[tls]]
  171. # ...

Configuration Mode

You have two choices:

To enable the file backend, you must either pass the --file option to the Traefik binary or put the [file] section (with or without inner settings) in the configuration file.

The configuration file allows managing both backends/frontends and HTTPS certificates (which are not Let's Encrypt certificates generated through Traefik).

TOML templating can be used if rules are not defined in the Traefik configuration file.

Rules in Traefik Configuration File

Add your configuration at the end of the global configuration file traefik.toml:

  1. defaultEntryPoints = ["http", "https"]
  2. [entryPoints]
  3. [entryPoints.http]
  4. # ...
  5. [entryPoints.https]
  6. # ...
  7. [file]
  8. # rules
  9. [backends]
  10. [backends.backend1]
  11. # ...
  12. [backends.backend2]
  13. # ...
  14. [frontends]
  15. [frontends.frontend1]
  16. # ...
  17. [frontends.frontend2]
  18. # ...
  19. [frontends.frontend3]
  20. # ...
  21. # HTTPS certificate
  22. [[tls]]
  23. # ...
  24. [[tls]]
  25. # ...

Note

If tls.entryPoints is not defined, the certificate is attached to all the defaultEntryPoints with a TLS configuration.

Note

Adding certificates directly to the entryPoint is still maintained but certificates declared in this way cannot be managed dynamically. It's recommended to use the file provider to declare certificates.

Warning

TOML templating cannot be used if rules are defined in the Traefik configuration file.

Rules in Dedicated Files

Traefik allows defining rules in one or more separate files.

One Separate File

You have to specify the file path in the file.filename option.

  1. # traefik.toml
  2. defaultEntryPoints = ["http", "https"]
  3. [entryPoints]
  4. [entryPoints.http]
  5. # ...
  6. [entryPoints.https]
  7. # ...
  8. [file]
  9. filename = "rules.toml"
  10. watch = true

The option file.watch allows Traefik to watch file changes automatically.

Multiple Separated Files

You could have multiple .toml files in a directory (and recursively in its sub-directories):

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

The option file.watch allows Traefik to watch file changes automatically.

Separate Files Content

If you are defining rules in one or more separate files, you can use two formats.

Simple Format

Backends, Frontends and TLS certificates are defined one at time, as described in the file rules.toml:

  1. # rules.toml
  2. [backends]
  3. [backends.backend1]
  4. # ...
  5. [backends.backend2]
  6. # ...
  7. [frontends]
  8. [frontends.frontend1]
  9. # ...
  10. [frontends.frontend2]
  11. # ...
  12. [frontends.frontend3]
  13. # ...
  14. # HTTPS certificate
  15. [[tls]]
  16. # ...
  17. [[tls]]
  18. # ...
TOML Templating

Warning

TOML templating can only be used if rules are defined in one or more separate files. Templating will not work in the Traefik configuration file.

Traefik allows using TOML templating.

Thus, it's possible to define easily lot of Backends, Frontends and TLS certificates as described in the file template-rules.toml :

  1. # template-rules.toml
  2. [backends]
  3. {{ range $i, $e := until 100 }}
  4. [backends.backend{{ $e }}]
  5. #...
  6. {{ end }}
  7. [frontends]
  8. {{ range $i, $e := until 100 }}
  9. [frontends.frontend{{ $e }}]
  10. #...
  11. {{ end }}
  12. # HTTPS certificate
  13. {{ range $i, $e := until 100 }}
  14. [[tls]]
  15. #...
  16. {{ end }}