skip to Main Content

I have 2 simple dockerized Rest Api apps which connect to external services and return json values. I try to deploy them behind traefik reverse proxy.

Here is docker-compose.yml for one Rest Api app:

version: "3.8"

services:
  first-rest-service:
    image: first-rest-service:latest
    container_name: first-rest-service
    restart: unless-stopped
    labels:
      - "traefik.http.routers.first-rest-service.rule=Host(`localhost`) && PathPrefix(`/first`)"
      - "traefik.http.services.first-rest-service.loadbalancer.server.port=8000"

networks:
  default:
    name: traefik
    external: true

Docker-compose.yml for second one app is almost the same (different image).
I try to deploy traefik as a reverse proxy. Here is docker-compose.yml for traefik:

version: "3.3"

services:

  traefik:
    image: "traefik:v2.10"
    container_name: "traefik"
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--api.debug=true"
      - "--log.level=DEBUG"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

networks:
  default:
    name: traefik
    external: true

After run all docker-composes I can see every single Rest Api app with proper forwarding in traefik dashboard. But when I execute curl on traefik I cannot get proper value. Example:

curl http://localhost/first/api/version
> {"detail":"Not Found"}

But when I remove PathPrefix and try to expose one Rest Api service on traefik it works well. I change only this line in Rest Api docker-compose.yml file:

      - "traefik.http.routers.first-rest-service.rule=Host(`localhost`) && PathPrefix(`/first`)"

into this:

      - "traefik.http.routers.first-rest-service.rule=Host(`localhost`)"

Here is output from curl:

curl http://localhost/api/version

I get json with proper version:

{"version":"x.x.x"}

I was trying to use PathPrefixStrip, but probably I cannot use it in proper way.
Maybe do I need change something in Rest Api routers settings?

2

Answers


  1. Chosen as BEST ANSWER

    I understood that my application has exposed as /api. So when I set PathPrefix to /first my curl to Rest Api App was /first/api/..., not /api/... as I expected. So I had to create middleware with stripprefix to rewrite /first/api/... into /api/....


  2. When you add PathPrefix to the router rule for your api service, traefik deliver full request (including prefix) to your service, so service can not recognize syntax.

    You can add traefik.http.middlewares.dell_d.stripprefix.prefixes

    More info https://doc.traefik.io/traefik/middlewares/http/stripprefix/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search