Stripprefix middleware not working as expected

Hello, I'm trying to replace caddy with traefik as my main reverse-proxy.
But I'm unable to do a simple PathPrefix / StripPrefix combination like i do in caddy with handle_path (https://caddyserver.com/docs/caddyfile/directives/handle_path)

My compose.yml

services:
  traefik:
    image: traefik:v3.1
    command:
      - --api.insecure=true
      - --providers.docker
      - --log.level=DEBUG
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  echo:
    image: mendhak/http-https-echo
    labels:
      traefik.http.routers.echo.rule: Host("localhost")

  echo-api:
    image: mendhak/http-https-echo
    labels:
      traefik.http.routers.echo-api.rule: Host("localhost") && PathPrefix("/api")
      traefik.http.routers.echo-api.middlewares": "strip-api"
      traefik.http.middlewares.strip-api.stripprefix.prefixes": "/api"

  echo-auth:
    image: mendhak/http-https-echo
    labels:
      traefik.http.routers.echo-auth.rule: Host("localhost") && PathPrefix("/auth")

Now if i do a curl http://localhost/auth/123 the request is routed to the echo-auth container, so far so good. But the request that the auth container sees has the /auth prefix still attached.

Now i want to remove this prefix for the echo-api container, but as soon as i do this, the requests are no longer routed to the echo-api container, instead they are catched by the echo container.

This is not what I'm expecting from this config, compare this caddy compose:

services:
  caddy:
    image: caddy:2
    ports:
      - "80:80"
    volumes:
      - "./Caddyfile:/etc/caddy/Caddyfile"
  echo:
    image: mendhak/http-https-echo
  echo-api:
    image: mendhak/http-https-echo
  echo-auth:
    image: mendhak/http-https-echo

With the following Caddyfile:

:80 {
	handle /auth* {
		reverse_proxy echo-auth:8080
	}

	handle_path /api* {
		reverse_proxy echo-api:8080
	}

	reverse_proxy echo:8080
}

Here the handle_path directive is stripping the /api prefix and routes the requests to the echo-api container and not to the echo container.

Can you please help me with this problem, as this is an essential config for a client/server deployment with traefik.

Regards

But echo-auth doesn’t have a stripprefix middleware attached.

Hi, yes i have the /auth container as a control group here, This container does not have its prefix stripped.
Also in the caddy example the prefix is not stripped as i use handle and not handle_path.

If you test both compose deployments and test them side-by-side you'll see they are the same for /auth (Prefix is not stripped)
But they differ for /api.
With caddy the /api prefix will get stripped correctly, but with traefik it will route to the echo container instead

I'll add the results of the caddy vs traefik tests, so you don't have to spin up the example:

caddy

curl localhost -> echo -> "/"
curl localhost/auth/test -> echo-auth -> "/auth/test"
curl localhost/api/test -> echo-api -> "/test"

traefik

curl localhost -> echo -> "/"
curl localhost/auth/test -> echo-auth -> "/auth/test"
curl localhost/api/test -> echo -> "/api/test" < This is unexpected

Maybe you need to fix your "s.

From the doc:

# Strip prefix /foobar and /fiibar
labels:
  - "traefik.http.middlewares.test-stripprefix.stripprefix.prefixes=/foobar,/fiibar"

I changed it to:

echo-api:
  image: mendhak/http-https-echo
  labels:
    - "traefik.http.routers.echo-api.rule=Host(`localhost`) && PathPrefix(`/api`)"
    - "traefik.http.routers.echo-api.middlewares=`strip-api`"
    - "traefik.http.middlewares.strip-api.stripprefix.prefixes=/api"

But getting the same results.
I think its an internal route ordering issue

Why backticks in there?

Enable Traefik dashboard and Traefik debug log to check if your config is correctly recognized by Traefik.