Traefik路由到单个域上的已知子域和通配符子域

时间:2018-09-19 17:54:28

标签: docker docker-compose traefik

是否可以有一个不包含特定子域的通配符子域?

*.mydomain.com OK
login.mydomain.com SKIP

在应用容器上使用通配符时,我无法访问登录容器。以下是我要完成的工作的图像。 (交通标志在技术上应该位于路线列表和集装箱之间)

Traefik Example Image

如果包含规则HostRegexp:{subdomain:[a-z]+}.${HOST_DOMAIN},则以下配置不起作用。

在除去通配符子域以外的所有内容的主机正则表达式之后,此配置成功运行。

services:
    traefik:
        image: traefik
        volumes:
        - /var/run/docker.sock:/var/run/docker.sock:ro
        - .traefik.toml:/etc/traefik/traefik.toml:ro
        ports:
        - "80:80"
        - "443:443"

    api:
        image: my-api-image
        labels:
        - "traefik.enable=true"
        - "traefik.port=80"
        - "traefik.frontend.rule=Host:app.${HOST_DOMAIN}; PathPrefix: /api"

    app:
        image: my-app-image
        labels:
        - "traefik.enable=true"
        - "traefik.port=80"
        - "traefik.frontend.rule=Host:app.${HOST_DOMAIN}"
        - "traefik.frontend.rule=HostRegexp:{subdomain:[a-z]+}.${HOST_DOMAIN}" # this second rule overwrites the first rule and I am aware of that, I am just showing what rules i've tried :)

    login:
        image: my-login-image
        labels:
        - "traefik.enable=true"
        - "traefik.port=80"
        - "traefik.frontend.rule=Host:login.${HOST_DOMAIN}"

我的问题当前是app容器。如果我将以下内容作为前端规则包括在内,则我的网关将出现问题:

"traefik.frontend.rule=HostRegexp:{subdomain:[a-z]+}.${HOST_DOMAIN}"

我还尝试将上述内容保留在应用程序下,并删除以下内容,但没有任何运气:

"traefik.frontend.rule=Host:app.${HOST_DOMAIN}"

任何建议或想法将不胜感激。谢谢。

编辑:

稍微改一下。

1 个答案:

答案 0 :(得分:5)

这对我有用:

version: '2'

services:
    traefik:
        image: traefik
        volumes:
        - /var/run/docker.sock:/var/run/docker.sock:ro
        # I removed your traefik.toml as you did not specify what is in it, so it's irrelevant
        ports:
        - "80:80"
        - "443:443"
        # Very helpful for debugging dashboard can be seen at http://localhost:8080 if the port is exposed
        - "8080:8080"
        labels:
        # You don't want traefik trying to create proxy for itself
        - "traefik.enable=false"
        # Since we have no traefik.toml any longer, let's put the essentials on the command line
        command: ["--api","--docker"]
    app:
        # this is my test image of a web server that dumps request back to the caller
        image: andrewsav/talkback
        # the hostname is a part of the dump, so let's specify something that we can relate to
        hostname: "app"
        labels:
        # note that you want this frontened to match the last. otherwise it will match login.${HOST_DOMAIN}"
        - "traefik.frontend.priority=1"
        - "traefik.enable=true"
        - "traefik.port=80"
        - "traefik.frontend.rule=HostRegexp:{subdomain:[a-z]+}.${HOST_DOMAIN}"
    api:
        image: andrewsav/talkback
        hostname: "api"
        labels:
        # this frontend needs to match before the one above
        - "traefik.frontend.priority=2"
        - "traefik.enable=true"
        - "traefik.port=80"
        - "traefik.frontend.rule=Host:app.${HOST_DOMAIN}; PathPrefix: /api"
    login:
        image: andrewsav/talkback
        hostname: "login"
        labels:
        - "traefik.frontend.priority=3"
        - "traefik.enable=true"
        - "traefik.port=80"
        - "traefik.frontend.rule=Host:login.${HOST_DOMAIN}"

一些注意事项:

  • 错误的网关表明您告诉traefik与之交谈的端点不在监听。查看仪表板,找出使用了哪个后端,然后再次检查ip / port是否正确。
  • 您必须使用优先级来匹配订单。请参阅the documentation以了解其工作方式。