Running multiple services with traefik docker on one domain, applies wrong routes

I'm trying to run two services, using one domain, but traefik redirects /api requests to interface service, even if I added PathPrefix, interface service logs 404 code when im trying to access api.

Here is my docker compose file:

services:
  traefik:
    image: "traefik:latest"
    command:
      - --log.level=ERROR
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --accesslog.format=json
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --entrypoints.web.http.redirections.entryPoint.to=websecure
      - --entrypoints.web.http.redirections.entryPoint.scheme=https
      - --entrypoints.web.http.redirections.entrypoint.permanent=true
      - --certificatesresolvers.my-resolver.acme.tlschallenge=true
      - --certificatesresolvers.my-resolver.acme.email=support@example.com
      - --certificatesresolvers.my-resolver.acme.storage=/letsencrypt/acme.json
    ports:
      - "80:80"
      - "8080:8080"
      - "443:443"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - ./certs:/letsencrypt

    depends_on:
      - api_server

  interface:
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.interface.rule=Host(`${DOMAIN}`)"
      - "traefik.http.routers.interface.entrypoints=websecure"
      - "traefik.http.routers.interface.tls.certresolver=my-resolver"
      - "traefik.http.services.interface.loadbalancer.server.port=80"

  api_server:
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.api.rule=Host(`${DOMAIN}`) && PathPrefix(`/api`)"
      - "traefik.http.routers.api.entrypoints=websecure"
      - "traefik.http.routers.api.tls.certresolver=my-resolver"
      - "traefik.http.services.api.loadbalancer.server.port=8000"

      - "traefik.http.routers.api.service=api"
      - "traefik.http.routers.api.middlewares=api_strip"
      - "traefik.http.middlewares.api_strip.stripprefix.prefixes=/api"
      - "traefik.http.middlewares.api_strip.stripprefix.forceslash=false"

In general it should work, the rule with /api is longer and therefore has higher priority, should be matched first.

Check Traefik dashboard or replace your target services with a whoami service, set hostname for each to differentiate containers.

Check simple Traefik example.

1 Like

Oh thanks, adding hostname: to each service fixed this issue!

services:
  interface:
    hostname: interface
    labels:

  api_server:
    hostname: api_server
    labels:

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