How to reverse proxy with path and query parameters for api

I'm trying to scale up my R plumber API's and starting playing with Traefik 2.0 as a means to expose, load balance, and secure the rest API's written in R. I can't get traefik to add basic auth and pass the path and query parameters to the container running my API. Basic auth works and I can set up the reverse proxy to the container correctly, but not both simultaneously. The setup below results in a 404 error.

I can run the containerized plumber API fine. I can access the containerized plumber API through traefik (seen below) using the trestletech/plumber docker image through the path "api_test". I can set up basic auth using Traefik's whoami example (seen below) and it works (ig http://localhost/traefexample/whoami).

I can't successfully add the two lines that are commented out below. I want those two lines to add basic auth, and while that works, I get a 404 error rather the getting the expected information.

version: '3.7'

services:
  reverse-proxy:
    # The official v2.0 Traefik docker image
    image: traefik:v2.0
    # Enables the web UI and tells Traefik to listen to docker
    command:
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --api.insecure=true
      - --providers.docker
      
    ports:
      # The HTTP port
      - "80:80"
      - "8000:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock

  api_test:
    # A container that exposes an API to show its IP address
    image: trestletech/plumber
    labels:
      - "traefik.http.routers.api_test.rule=Path(`/api_test/{path:.+}`)"
      - "traefik.http.middlewares.api_test-replacepathregex.replacepathregex.regex=/api_test(.*)"
      - "traefik.http.middlewares.api_test-replacepathregex.replacepathregex.replacement=$$1"
      - "traefik.http.routers.api_test.middlewares=api_test-replacepathregex@docker"
   #   - "traefik.http.routers.api_test.middlewares=auth"
   #   - "traefik.http.middlewares.auth.basicauth.users=user:$$apr1$$q8eZFHjF$$Fvmkk//V6Btlaf2i/ju5n/"


  traefexample:
  # A container that exposes an API to show its IP address example from traefik
    image: containous/whoami
    labels:
      - "traefik.http.routers.traefexample.rule=Path(`/traefexample/{path:.+}`)"
      - "traefik.http.middlewares.traefexample-replacepathregex.replacepathregex.regex=/traefexample(.*)"
      - "traefik.http.middlewares.traefexample-replacepathregex.replacepathregex.replacement=$$1"
      - "traefik.http.routers.traefexample.middlewares=traefexample-replacepathregex@docker"
      - "traefik.http.routers.traefexample.middlewares=auth"
      - "traefik.http.middlewares.auth.basicauth.users=user:$$apr1$$q8eZFHjF$$Fvmkk//V6Btlaf2i/ju5n/"

With the posted setup, I can use the endpoint "http://localhost/api_test/mean" and the response is the mean of some randomly selected numbers.

When I add the two auth lines (currently commented out), I get the 404 error when I expect a single number.

You result makes sense: when you use replacepathregex middleware you get your desired path translation, when you use basicauth middleware, you get your authentication.

You just need to use both at the same time instead of replacing one with the other.

Look at documentation for example. Here is the reference: https://docs.traefik.io/reference/dynamic-configuration/docker/