Skip to content

Traefik & File

旧配置文件

文件提供程序允许您在 TOML 或 YAML 文件中定义动态配置。您可以编写以下配置元素:

注意

文件提供程序是整个文档中使用的默认格式,用于显示许多功能的配置示例。

提示

文件提供程序可以是您希望从其他提供程序重用的常用元素的良好位置; 例如,声明白名单中间件,基本身份验证,......

配置示例

声明路由器,中间件和服务

启用文件提供程序:

[providers.file]
  filename = "/my/path/to/dynamic-conf.toml"
providers:
  file:
    filename: "/my/path/to/dynamic-conf.yml"
--providers.file.filename=/my/path/to/dynamic_conf.toml

声明路由器,中间件和服务:

[http]
  # Add the router
  [http.routers]
    [http.routers.router0]
      entryPoints = ["web"]
      middlewares = ["my-basic-auth"]
      service = "service-foo"
      rule = "Path(`foo`)"

    # Add the middleware
    [http.middlewares]
      [http.middlewares.my-basic-auth.basicAuth]
        users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
                  "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"]
        usersFile = "etc/traefik/.htpasswd"

    # Add the service
    [http.services]
      [http.services.service-foo]
        [http.services.service-foo.loadBalancer]
          [[http.services.service-foo.loadBalancer.servers]]
            url = "http://foo/"
          [[http.services.service-foo.loadBalancer.servers]]
            url = "http://bar/"
http:
  # Add the router
  routers:
    router0:
      entryPoints:
      - web
      middlewares:
      - my-basic-auth
      service: service-foo
      rule: Path(`foo`)

  # Add the middleware
  middlewares:
    my-basic-auth:
      basicAuth:
        users:
        - test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/
        - test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0
        usersFile: etc/traefik/.htpasswd

  # Add the service
  services:
    service-foo:
      loadBalancer:
        servers:
        - url: http://foo/
        - url: http://bar/
        passHostHeader: false

提供者配置选项

浏览参考

如果你赶时间,也许你宁愿通过静态动态配置参考。

filename

Optional

定义配置文件的路径。

[providers]
  [providers.file]
    filename = "dynamic_conf.toml"
providers:
  file:
    filename: dynamic_conf.yml
--providers.file.filename=dynamic_conf.toml

directory

Optional

定义包含配置文件的目录。

[providers]
  [providers.file]
    directory = "/path/to/config"
providers:
  file:
    directory: /path/to/config
--providers.file.directory=/path/to/config

watch

Optional

watch选项设置true为允许Traefik自动监视文件更改。 它适用于filenamedirectory选项。

[providers]
  [providers.file]
    filename = "dynamic_conf.toml"
    watch = true
providers:
  file:
    filename: dynamic_conf.yml
    watch: true
--providers.file.filename=dynamic_conf.toml
--providers.file.watch=true

模板

Warning

Go Templating 仅适用于专用配置文件。模板在 Traefik 主配置文件中不起作用。

Traefik 允许使用 Go 模板。 因此,可以轻松定义许多路由器,服务和 TLS 证书,如文件中所述template-rules.toml

使用模板配置
# template-rules.toml
[http]

  [http.routers]
  {{ range $i, $e := until 100 }}
    [http.routers.router{{ $e }}]
    # ...
  {{ end }}  


  [http.services]
  {{ range $i, $e := until 100 }}
      [http.services.service{{ $e }}]
      # ...
  {{ end }}  

[tcp]

  [tcp.routers]
  {{ range $i, $e := until 100 }}
    [tcp.routers.router{{ $e }}]
    # ...
  {{ end }}  


  [tcp.services]
  {{ range $i, $e := until 100 }}
      [http.services.service{{ $e }}]
      # ...
  {{ end }}  

{{ range $i, $e := until 10 }}
[[tls.certificates]]
  certFile = "/etc/traefik/cert-{{ $e }}.pem"
  keyFile = "/etc/traefik/cert-{{ $e }}.key"
  store = ["my-store-foo-{{ $e }}", "my-store-bar-{{ $e }}"]
{{ end }}

[tls.config]
{{ range $i, $e := until 10 }}
  [tls.config.TLS{{ $e }}]
  # ...
{{ end }}
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:
  - certFile: "/etc/traefik/cert-{{ $e }}.pem"
    keyFile: "/etc/traefik/cert-{{ $e }}.key"
    store:
    - "my-store-foo-{{ $e }}"
    - "my-store-bar-{{ $e }}"
{{end}}