全局使用 traefik 中间件

Ral*_*egi 3 docker traefik

我正在尝试在traefik.yml文件中声明 https 重定向。现在我尝试在.traefik服务中添加这些规则docker-compose.yml。这就像一个魅力。虽然我更喜欢在traefik.yml文件中配置这个全局和中间件重定向,然后只在 .traefik 服务中引用它docker-compose.yml

我以前吃过的

version: '3'

networks:
  web:
    external: true

services:
  traefik:
    image: traefik:v2.1
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./.traefik/traefik.yml:/traefik.yml
      - ./.traefik/acme.json:/acme.json
    networks:
      - web
    labels:
       - "traefik.enable=true"
       - "traefik.http.routers.traefik.rule=Host(`$HOSTNAME`)"
       - "traefik.http.routers.traefik.service=api@internal"
       - "traefik.http.routers.traefik.tls.certresolver=le"
       - "traefik.http.routers.traefik.entrypoints=https"
       # Global redirect to https
       - "traefik.http.routers.http-catchall.rule=hostregexp(`{host:.+}`)"
       - "traefik.http.routers.http-catchall.entrypoints=http"
       - "traefik.http.routers.http-catchall.middlewares=redirect-to-https"
       # Middleware redirect
       - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
Run Code Online (Sandbox Code Playgroud)

这很容易,并将所有其他域从 http 重定向到 https。

我现在想要什么

我想在traefik.yml.

到目前为止,我已经这样做了。

api: {}

entryPoints:
  http:
    address: ":80"
  https:
    address: ":443"

log:
  level: DEBUG

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
    network: web

http:
  # Global redirect to https
  routers:
    http-catchall:
      rule: hostregexp(`{host:.+}`)"
      entrypoints:
        http
      middlewares:
        - redirect-to-https
  # Middleware redirect
  middlewares:
    redirect-to-https:
      redirectScheme:
        scheme: https

certificatesResolvers:
  le:
    acme:
      email: john@doe.com
      storage: acme.json
      # Activate for Development: Certificate will not be valid. It's only for testing if it can be obtained.
      #caServer: https://acme-staging-v02.api.letsencrypt.org/directory
      httpChallenge:
        entryPoint: http
Run Code Online (Sandbox Code Playgroud)

如您所见,我声明了 http 设置。

我现在的问题是如何将这些设置引用到我的 traefik 服务中?

我试过了

- "traefik.http.middlewares=redirect-to-https"

- "traefik.http.middlewares.redirect-to-https"

- "traefik.http.middlewares.traefik=redirect-to-https@file"

他们都没有工作。有些在仪表板中显示中间件,但它没有链接到任何设置。

有没有人找到解决方案?我无法从有关此的文档中获得任何信息。我认为它必须以某种方式链接到@file

谢谢

Vas*_*ryk 6

事实上,你不需要在labels块中将这个中间件设置为traefik。如果你的目录中有这样的配置traefik.yml

http:
  routers:
    http-catchall:
      rule: hostregexp(`{host:.+}`)
      entrypoints:
        - http
      middlewares:
        - redirect-to-https

  middlewares:
    redirect-to-https:
      redirectScheme:
        scheme: https
        permanent: false
Run Code Online (Sandbox Code Playgroud)

这意味着 - 到达入口点的所有流量http都应该使用中间件redirect-to-https并重定向到另一个入口点:https。此配置是全局的。

因此,您只需将容器设置为https入口点(就像您在示例中所做的那样)

labels:
  - "traefik.enable=true"
  - "traefik.http.routers.traefik.entrypoints=https"
  - "traefik.http.routers.traefik.rule=Host(`traefik.mydomain.ua`)"
  - "traefik.http.routers.traefik.tls=true"
  - "traefik.http.routers.traefik.tls.certresolver=letsEncrypt"
  - "traefik.http.routers.traefik.service=api@internal"
Run Code Online (Sandbox Code Playgroud)


Ein*_*nar 5

@file 意味着中间件是在文件提供程序中定义的。

您可以在 traefik.yml 中添加这样的文件提供程序。

providers:
  file:
    directory: "/path/to/dynamic/conf"
Run Code Online (Sandbox Code Playgroud)

使用中间件在该目录中创建一个文件。

http:
  middlewares:
    redirect-to-https:
      redirectScheme:
        scheme: https
Run Code Online (Sandbox Code Playgroud)

您现在可以在标签中引用重定向到 https@file。

- "traefik.http.middlewares.traefik=redirect-to-https@file"
Run Code Online (Sandbox Code Playgroud)

注意: traefik.yml 中的某些配置可能需要移动到新的 yml 文件中。我是 Traefik 的新手,还不完全了解原因。

请参阅文档中的以下部分: