Traefik routing "not working"

Hello,

In the same server I have the client (nextjs) and an API (fastapi).
The client has been redirected to localhost:

...
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.client.service=client"
      - "traefik.http.routers.client.entrypoints=web"
      - "traefik.http.services.client.loadbalancer.server.port=3000"
      - "traefik.http.routers.client.rule=Host(`${DOMAIN?Variable not set}`) && Path(`/`)"

where DOMAIN is localhost in my .env

My API config is:

    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.api.service=api"
      - "traefik.http.routers.api.entrypoints=web"
      - "traefik.http.services.api.loadbalancer.server.port=8000"
      - "traefik.http.routers.api.rule=Host(`${DOMAIN?Variable not set}`) && PathPrefix(`/api`)"

So, I'm routing my client container to localhost/ (this IS working) and my API to localhost/api (this IS NOT working).

When I access the logs in the API container, I saw the fastapi is trying to access the route /api of application thas doesn't exists.

The logs:

2025-01-16 11:46:56 INFO:     Started server process [19]
2025-01-16 11:46:56 INFO:     Waiting for application startup.
2025-01-16 11:46:56 INFO:     Application startup complete.
2025-01-16 11:46:56 INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
2025-01-16 11:47:20 INFO:     172.18.0.6:52670 - **"GET /api HTTP/1.1"** 404 Not Found
2025-01-16 11:47:20 INFO:     172.18.0.6:52670 - **"GET /api HTTP/1.1"** 404 Not Found

Of course that will not work because this route doesn't exists in my API.
I wish traefik understand that /api means the route / in my API.

Am I loosing something?

Share your full Traefik static and dynamic config, and docker-compose.yml if used.

Enable and check Traefik debug log (doc) and Traefik access log in JSON format (doc).

1 Like

Thank you for your response!

Actually I got the mistake! It's necessary to add a middleware, like that:

...
labels:
      - ...
      - "traefik.http.middlewares.strip-api.stripprefix.prefixes=/api"
      - "traefik.http.routers.api.middlewares=strip-api"

This middleware will strip the /api from request!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.