Forward traffic from path to service

Hello!

I have a docker-compose config that looks like this:

  core:
    labels:
      - "traefik.http.routers.core.rule=(Host(`${CORE_URL}`) && PathPrefix(`/server/`))"
      - "traefik.http.services.core.loadbalancer.server.port=8080"

From my understanding, this is supposed to forward all traffic from <core_url>/server/* to the core service, however it only gives me "Gateway Timeout".

if i only use:

  core:
    labels:
      - "traefik.http.routers.core.rule=(Host(`${CORE_URL}`))"
      - "traefik.http.services.core.loadbalancer.server.port=8080"

It works, but i need two services on the same url.

Any idea what could be wrong?

Hello,

you must strip the prefix you added before sending the request to your application.

https://docs.traefik.io/v2.1/middlewares/stripprefix/

Thank you!

This worked! Sort of. I think i was a bit unclear in my initial post, so i will elaborate.
I have two services, a nginx frontend which will be hosted at CORE_URL/, and then a bunch of endpoints which should forward traffic from, for example CORE_URL/server/* or CORE_URL/login to service/login or service/server/*.

Using stripprefix worked when i only had a single service, but not now with two.

My current config (i havent added all endpoints yet) looks similar to this:

  core:
    labels:
      - "traefik.http.routers.core.rule=Host(`${CORE_URL}`) && PathPrefix(`/server`)"
      - "traefik.http.services.core.loadbalancer.server.port=8080"
      - "traefik.http.middlewares.core.stripprefix.prefixes=/server"

  frontend:
    labels:
      - "traefik.http.routers.frontend.rule=Host(`${CORE_URL}`)"
      - "traefik.http.services.frontend.loadbalancer.server.port=80"

The he link between middleware definition and must be define explicitly:

  core:
    labels:
      - "traefik.http.routers.core.rule=Host(`${CORE_URL}`) && PathPrefix(`/server`)"
      # uses the middleware on the router
      - "traefik.http.routers.core.middlewares=core"
      - "traefik.http.services.core.loadbalancer.server.port=8080"
      # middleware definition
      - "traefik.http.middlewares.core.stripprefix.prefixes=/server"

  frontend:
    labels:
      - "traefik.http.routers.frontend.rule=Host(`${CORE_URL}`)"
      - "traefik.http.services.frontend.loadbalancer.server.port=80"

I am still having trouble accessing core_url/server/ping.json.

  core:
    labels:
      - "traefik.http.routers.core.rule=Host(`${CORE_URL}`) && PathPrefix(`/server`)"
      - "traefik.http.services.core.loadbalancer.server.port=8080"
      - "traefik.http.routers.core.middlewares=core"
      - "traefik.http.middlewares.core.stripprefix.prefixes=/server"

I also tried using stripprefixregex=/server/* to no avail.

any suggestions?

version: '3.7'

services:

  traefik:
    image: traefik:v2.1.6
    command:
      - --log.level=INFO
      - --api
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    labels:
      traefik.enable: true

      # Dashboard
      traefik.http.routers.traefik.rule: Host(`traefik.localhost`)
      traefik.http.routers.traefik.entrypoints: web
      traefik.http.routers.traefik.service: api@internal

  core:
    image: containous/whoami:v1.4.0
    command:
      # It tells whoami to start listening on 8080 instead of 80
      - --port=8080
    labels:
      traefik.enable: true

      traefik.http.routers.core.rule: Host(`core.localhost`) && PathPrefix(`/server`)
      traefik.http.routers.core.entrypoints: web
      # uses the middleware on the router
      traefik.http.routers.core.middlewares: strip

      traefik.http.services.core.loadbalancer.server.port: 8080
      
      # middleware definition
      traefik.http.middlewares.strip.stripprefix.prefixes: /server

  frontend:
    image: containous/whoami:v1.4.0
    labels:
      traefik.enable: true

      traefik.http.routers.frontend.rule: Host(`core.localhost`)
      traefik.http.routers.frontend.entrypoints: web

Thank you very much for this!

Looks very similar to what i want, but i now get 404 on /server/ping.json