【问题标题】:Use traefik middleware globally全局使用 traefik 中间件
【发布时间】:2020-01-27 11:02:12
【问题描述】:

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

我之前的经历

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"

这很容易,并将所有其他域从 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

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

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

我试过了

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

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

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

它们都不起作用。有些在仪表板中显示中间件,但未链接到任何设置。

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

谢谢

【问题讨论】:

  • 嗨,你找到解决办法了吗?
  • @jujule 实际上我没有进一步投资。我们只是在每个项目的 docker-compose.yml 文件中声明它

标签: docker traefik


【解决方案1】:

@file 表示在文件提供者中定义了中间件。

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

providers:
  file:
    directory: "/path/to/dynamic/conf"

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

http:
  middlewares:
    redirect-to-https:
      redirectScheme:
        scheme: https

您现在可以在标签中引用 redirect-to-https@file。

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

注意:您在 traefik.yml 中的一些配置可能需要移动到新的 yml 文件中。我是 Traefik 的新手,还不完全清楚为什么。

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

【讨论】:

    【解决方案2】:

    其实你不需要在标签块中设置这个中间件到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
    

    这意味着 - 所有到达入口点 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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 2012-10-13
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多