Search code examples
traefik

Define host and path frontend rule for Traefik


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.

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

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.

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?


Solution

  • 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
    

    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.

    Update: Traefik v2

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

    See the docs