Traefik using diff paths for diff containers

Hello,

I am pretty new to Traefik so apologies upfront if this is too simple.
I have gone through the Traefik documentation as well as this communities posts, but could not find an answer to what I am looking for.

My setup is as follows:

  • Container 1 runs WebApp1, listens on port 80 default path of web application is /
  • Container 2 runs WebApp2, listens on port 80 default path of web application is /
  • I want to use Traefik to route requests to each web app using different paths (that don't necessarily exist on the web applications)

http://host.domain/webapp1 requests go to Container 1. I would need to strip the prefix since the web application is not hosted at /webapp1
http://host.domain/webapp2 requests go to Container 2. Same strip would have to be done here as well.

I have tried the addpath and strippath middlewares, but it did not work like I expected. Can Traefik handle this type of flow?

Hi @runelucica

Yes, this some of the basic functionality.

Post your configs or representative configs and folks here can help in a more target fashion.

Sorry for the delay.

Here is the sample config I tried that did not work:

traefik-reverse-proxy:
    # The official v2 Traefik docker image
    image: traefik:v2.1
    # Enables the web UI and tells Traefik to listen to docker
    command: 
      # Enabled Traefik logging
      - "--accesslog=true"
      - "--log=true"
      - "--log.level=debug"
      # Traefik will listen on port 8080 by default for API request.
      - "--api.insecure=true"
      # Enabling docker provider
      - "--providers.docker=true"
      # Do not expose containers unless explicitly told so
      - "--providers.docker.exposedbydefault=false"
      # Traefik will listen to incoming request on entry point named web on port 80 (HTTP)
      - "--entrypoints.web.address=:80"
      # Traefik will listen to incoming request on entry point named websecure on port 443 (HTTPS)
      - "--entrypoints.websecure.address=:443"    
    volumes:
      # So that Traefik can listen to the Docker events
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    ports:
      # The HTTP port
      - "80:80"
      # The HTTPS port
      - "443:443"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"

web-app1:
    image: "some-image"
    container_name: "web-app1"
	labels:
      - "traefik.enable=true"
      # Define router for web-app1
	  - "traefik.http.routers.web-app1-router.rule=Host(`hostname.domain`)&&Path(`/web-app1`)"
      - "traefik.http.routers.web-app1-router.entrypoints=web,websecure"
	  - "traefik.http.routers.web-app1-router.tls=true"
      - "traefik.http.services.web-app1-router.loadbalancer.server.port=80"
      # Define middleware for web-app1
	  - "traefik.http.middlewares.web-app1-strip-prefix.stripprefix.prefixes=/web-app1"
      # Apply middleware to router
	  - "traefik.http.routers.web-app1-router.middlewares=web-app1-strip-prefix@docker"

web-app2:
    image: "some-image"
    container_name: "web-app2"
	labels:
      - "traefik.enable=true"
      # Define router for web-app2
	  - "traefik.http.routers.web-app2-router.rule=Host(`hostname.domain`)&&Path(`/web-app2`)"
      - "traefik.http.routers.web-app2-router.entrypoints=web,websecure"
	  - "traefik.http.routers.web-app2-router.tls=true"
      - "traefik.http.services.web-app2-router.loadbalancer.server.port=80"
      # Define middleware for web-app2
	  - "traefik.http.middlewares.web-app2-strip-prefix.stripprefix.prefixes=/web-app2"
      # Apply middleware to router
	  - "traefik.http.routers.web-app2-router.middlewares=web-app2-strip-prefix@docker"

I was able to get this to work by using the hostname (using subdomains) as the decision point for selecting the service, but I would like to avoid this if possible:

web-app1:
    image: "some-image"
    container_name: "web-app1"
	labels:
	  - "traefik.enable=true"
      - "traefik.http.routers.web-app1-router.rule=Host(`web-app1.hostname.domain`)"
      - "traefik.http.routers.web-app1-router.entrypoints=web,websecure"
      - "traefik.http.routers.web-app1-router.tls=true"
      - "traefik.http.services.web-app1-router.loadbalancer.server.port=80"

Thanks

For a reverse Proxy, yes. I have the same Problem with Traefik.

Starting Traefik:

  reverse-proxy:
    image: "traefik:latest"
    command:
      - --entrypoints.web.address=:80
      - --providers.docker=true
      - --api.insecure
      - --providers.docker.useBindPortIP=true
      - --providers.docker.exposedByDefault=false
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

Only one Page example works fine:

  info1:
    image: httpd:alpine
    labels:
      traefik.http.routers.main.rule: "path(`/a`)"
      traefik.http.routers.main.middlewares: stripprefix
      traefik.http.middlewares.stripprefix.stripprefix.prefixes: /a

It also works, if the container knows the "Route": (That's container command, no general usage possible)

  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9090:9090"
    command:
      - --config.file=/etc/prometheus/prometheus.yml
      - --web.enable-lifecycle
      - --web.external-url=http://"SERVER"."COMPANYDOMAIN".com/prometheus
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.prometheus.rule=PathPrefix(`/prometheus`)"

BUT it fail on a "generic" solution:

  pylon:
    image: "linuxserver/pylon"
    container_name: pylon
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Europe/London
      - PYUSER=test #optional
      - PYPASS=test123 #optional
    ports:
      - 3131:3131
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.pylon.rule=PathPrefix(`/pylon`)"
      - "traefik.http.middlewares.pylonpathstrip.stripprefix.prefixes=/pylon"
      - "traefik.http.routers.pylon.middlewares=pylonpathstrip@docker"
      - "traefik.http.services.pylon.loadbalancer.server.port=3131"

Login works, but all subresources fail to load.
image

With subdomains it works perfect, but with subpaths...
Our problem is, the Server is on a subdomain: server1.companyA.com
SubSubDomain is not working: dockerA.server1.companyA.com
Only solution: server1.companyA.com/dockerA

Aside from the indenting(hooing that is just copy/paste error) about the only thing I'd say is drop the @docker off the ...middlewares=

As you have accesslog and debug enabled. You should be able to see what is happening with your requests.

1 Like

The indenting is indeed the outcome of copy/pasting.
I will drop the @docker and test again.