为 Traefik 定义主机和路径前端规则 [英] Define host and path frontend rule for Traefik

查看:29
本文介绍了为 Traefik 定义主机和路径前端规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Traefik 在我的 Docker Swarm 模式集群中部署代理多个应用程序.

I am trying to use Traefik to deploy proxy multiple applications in my Docker Swarm mode cluster.

我已经得到它以便它代理一个命名的主机,但我希望它代理一个命名的主机和路径,但我无法计算出我需要使用的标签.

I have got it so that it proxies a named Host but I want it to proxy on a named Host and Path, but I cannot work out the labels I need to use.

这是我正在使用的 docker service 命令:

This is the docker service command I am using:

 docker service create \
                       \
    --label "traefik.port=9000" \
    --label "traefik.docker.network=traefik-net" \
    --label "traefik.frontend.rule=Host:`hostname -f`" \
    --label="traefik.backend=portainer" \
                                        \
    --constraint "node.role == manager" \
    -p 9000:9000 \
    --mount "type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock" \
    --name portainer \
    portainer/portainer

如果主机是 dummy.localhost,那么我可以在 http://dummy.localhost 上点击 portainer 应用程序.但是我想修改它,以便我必须使用 http://dummy.localhost/portiner.

If the host is dummy.localhost then I am able to hit the portainer app on http://dummy.localhost. However I want to modify it so that I have to use http://dummy.localhost/portainer.

我已经看到在为 Traefik 使用 toml 文件时有一些方法可以做到这一点,但我在部署的 docker 服务上使用了监视模式和标签.

I have seen that there are ways to do this when using a toml file for Traefik, but I am using watch mode and labels on the docker services I deploy.

如何在标签中组合多个前端规则,以便可以在主机名和路径上代理此(以及任何其他)应用程序?

How can I combine multiple front end rules in my labels so that this (and any other) application can be proxied on a hostname and a path?

推荐答案

Traefik v1

如果您希望应用多个规则以使路由决策生效,请用分号分隔它们.例如:

Traefik v1

If you want multiple rules to apply in order for a routing decision to become effective, separate them by semicolon. For instance:

Host: <your host rule>; PathPrefixStrip: /portainer

上面的意思是:如果主机和路径前缀匹配,Traefik 会将请求路由到关联的后端(并在转发之前去除指定的路径前缀).这甚至在标签内定义时也有效.

What the above means is: If the host and path prefix match, Traefik will route requests to the associated backend(s) (and strip off the specified path prefix prior to forwarding). This even works when defined inside a label.

有关详细信息,请参阅前端文档.

See the frontend documentation for details.

Host(`domain.com`) && Path(`/path`)

请参阅文档

这篇关于为 Traefik 定义主机和路径前端规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆