traefik/docs/content/providers/file.md

270 lines
6.2 KiB
Markdown
Raw Normal View History

2019-03-14 15:46:05 +00:00
# Traefik & File
Good Old Configuration File
{: .subtitle }
The file provider lets you define the [dynamic configuration](./overview.md) in a TOML or YAML file.
2019-03-14 15:46:05 +00:00
You can write these configuration elements:
2019-04-24 15:44:04 +00:00
* In [a dedicated file](#filename)
* In [several dedicated files](#directory)
2019-03-14 15:46:05 +00:00
!!! note
The file provider is the default format used throughout the documentation to show samples of the configuration for many features.
2019-03-14 15:46:05 +00:00
!!! 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, ...
2019-03-14 15:46:05 +00:00
## Configuration Examples
2019-03-14 15:46:05 +00:00
??? example "Declaring Routers, Middlewares & Services"
Enabling the file provider:
```toml tab="File (TOML)"
2019-03-21 14:34:04 +00:00
[providers.file]
filename = "/my/path/to/dynamic-conf.toml"
```
```yaml tab="File (YAML)"
providers:
file:
filename: "/my/path/to/dynamic-conf.yml"
```
```bash tab="CLI"
--providers.file.filename=/my/path/to/dynamic_conf.toml
```
2019-03-14 15:46:05 +00:00
Declaring Routers, Middlewares & Services:
```toml tab="TOML"
2019-03-14 15:46:05 +00:00
[http]
# Add the router
[http.routers]
[http.routers.router0]
entryPoints = ["web"]
2019-03-14 15:46:05 +00:00
middlewares = ["my-basic-auth"]
service = "service-foo"
2019-09-03 16:02:05 +00:00
rule = "Path(`/foo`)"
2019-03-14 15:46:05 +00:00
# Add the middleware
[http.middlewares]
2019-07-01 09:30:05 +00:00
[http.middlewares.my-basic-auth.basicAuth]
2019-03-14 15:46:05 +00:00
users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"]
usersFile = "etc/traefik/.htpasswd"
# Add the service
[http.services]
[http.services.service-foo]
2019-07-01 09:30:05 +00:00
[http.services.service-foo.loadBalancer]
[[http.services.service-foo.loadBalancer.servers]]
2019-03-14 15:46:05 +00:00
url = "http://foo/"
2019-07-01 09:30:05 +00:00
[[http.services.service-foo.loadBalancer.servers]]
2019-03-14 15:46:05 +00:00
url = "http://bar/"
```
```yaml tab="YAML"
http:
2019-07-01 09:30:05 +00:00
# Add the router
routers:
router0:
2019-07-01 09:30:05 +00:00
entryPoints:
- web
middlewares:
- my-basic-auth
service: service-foo
2019-09-03 16:02:05 +00:00
rule: Path(`/foo`)
2019-07-01 09:30:05 +00:00
# Add the middleware
middlewares:
my-basic-auth:
basicAuth:
users:
- test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/
- test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0
2019-07-01 09:30:05 +00:00
usersFile: etc/traefik/.htpasswd
# Add the service
services:
service-foo:
2019-07-01 09:30:05 +00:00
loadBalancer:
servers:
- url: http://foo/
- url: http://bar/
2019-07-01 09:30:05 +00:00
passHostHeader: false
```
2019-03-14 15:46:05 +00:00
## Provider Configuration Options
2019-03-14 15:46:05 +00:00
!!! tip "Browse the Reference"
If you're in a hurry, maybe you'd rather go through the [static](../reference/static-configuration/overview.md) and the [dynamic](../reference/dynamic-configuration/file.md) configuration references.
2019-04-24 15:44:04 +00:00
### `filename`
_Optional_
2019-03-14 15:46:05 +00:00
Defines the path of the configuration file.
```toml tab="File (TOML)"
2019-03-14 15:46:05 +00:00
[providers]
[providers.file]
filename = "dynamic_conf.toml"
2019-03-14 15:46:05 +00:00
```
```yaml tab="File (YAML)"
providers:
file:
filename: dynamic_conf.yml
```
```bash tab="CLI"
--providers.file.filename=dynamic_conf.toml
```
2019-04-24 15:44:04 +00:00
### `directory`
_Optional_
2019-03-14 15:46:05 +00:00
Defines the directory that contains the configuration files.
```toml tab="File (TOML)"
2019-03-14 15:46:05 +00:00
[providers]
[providers.file]
directory = "/path/to/config"
```
```yaml tab="File (YAML)"
providers:
file:
directory: /path/to/config
```
```bash tab="CLI"
--providers.file.directory=/path/to/config
```
2019-04-24 15:44:04 +00:00
### `watch`
_Optional_
2019-03-14 15:46:05 +00:00
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.
```toml tab="File (TOML)"
2019-03-14 15:46:05 +00:00
[providers]
[providers.file]
filename = "dynamic_conf.toml"
2019-03-14 15:46:05 +00:00
watch = true
```
```yaml tab="File (YAML)"
providers:
file:
filename: dynamic_conf.yml
watch: true
```
```bash tab="CLI"
--providers.file.filename=dynamic_conf.toml
--providers.file.watch=true
```
### Go Templating
2019-03-14 15:46:05 +00:00
!!! warning
Go Templating only works along with dedicated configuration files.
Templating does not work in the Traefik main configuration file.
2019-03-14 15:46:05 +00:00
Traefik allows using Go templating.
2019-03-14 15:46:05 +00:00
Thus, it's possible to define easily lot of routers, services and TLS certificates as described in the file `template-rules.toml` :
??? example "Configuring Using Templating"
```toml tab="TOML"
2019-03-14 15:46:05 +00:00
# template-rules.toml
[http]
[http.routers]
{{ range $i, $e := until 100 }}
[http.routers.router{{ $e }}]
# ...
{{ end }}
2019-07-01 09:30:05 +00:00
[http.services]
2019-03-14 15:46:05 +00:00
{{ range $i, $e := until 100 }}
[http.services.service{{ $e }}]
# ...
{{ end }}
[tcp]
[tcp.routers]
{{ range $i, $e := until 100 }}
[tcp.routers.router{{ $e }}]
# ...
{{ end }}
2019-07-01 09:30:05 +00:00
[tcp.services]
2019-03-14 15:46:05 +00:00
{{ range $i, $e := until 100 }}
[http.services.service{{ $e }}]
# ...
{{ end }}
{{ range $i, $e := until 10 }}
[[tls.certificates]]
2019-07-01 09:30:05 +00:00
certFile = "/etc/traefik/cert-{{ $e }}.pem"
keyFile = "/etc/traefik/cert-{{ $e }}.key"
store = ["my-store-foo-{{ $e }}", "my-store-bar-{{ $e }}"]
2019-03-14 15:46:05 +00:00
{{ end }}
[tls.config]
2019-03-14 15:46:05 +00:00
{{ range $i, $e := until 10 }}
[tls.config.TLS{{ $e }}]
2019-03-14 15:46:05 +00:00
# ...
{{ end }}
```
```yaml tab="YAML"
http:
{{range $i, $e := until 100 }}
routers:
router{{ $e }:
# ...
{{end}}
{{range $i, $e := until 100 }}
services:
application{{ $e }}:
# ...
{{end}}
tcp:
{{range $i, $e := until 100 }}
routers:
router{{ $e }:
# ...
{{end}}
{{range $i, $e := until 100 }}
services:
service{{ $e }}:
# ...
{{end}}
{{ range $i, $e := until 10 }}
tls:
certificates:
2019-07-01 09:30:05 +00:00
- certFile: "/etc/traefik/cert-{{ $e }}.pem"
keyFile: "/etc/traefik/cert-{{ $e }}.key"
store:
- "my-store-foo-{{ $e }}"
- "my-store-bar-{{ $e }}"
{{end}}
```