1

What I am trying to do:

I have a docker-compose file with 4 containers traefik, UI, API and DB.

I want to use traefik to redirect both containers UI and API to port 80 but on a different url path. UI is redirected directly to localhost:80 and API I want to redirect to localhost/api, but the problem is that localhost/api is already taken in the UI container by valid html code. So I need to exclude it from the API routing or provide a valid PathRegexp which I am failing to create.

Here is the docker-compose where Iam trying PathRegexp:

Idea is to redirect only api endpointslike /api/endpoint1 or /api/endpoint2 but not redirect /api itself as that one I need to be redirected to UI container.

version: "3"

services:

  api:
    build:
      context: .
      dockerfile: ./api/dockerfile
    depends_on:
      - db
      - traefik
    labels:
      - "traefik.http.routers.api.rule=Host(`localhost`) && PathRegexp(`^/api/.*`)"
    ports:
      - 5000:5000

  ui:
    build:
      context: .
      dockerfile: ./ui/dockerfile
    depends_on:
      - api
      - db
      - traefik
    volumes:
      - ./ui/html/:/var/www/html
    labels:
      - "traefik.http.routers.ui.rule=Host(`localhost`)"
    ports:
      - 81:81
      - 443:443

  db:
    image: mysql
    restart: always
    env_file: .env
    environment:
        MYSQL_ROOT_PASSWORD: $ROOT_PASS_MYSQL
        MYSQL_DATABASE: $PRACTICE_DB
    ports:
      - 3307:3306
    volumes:
      - ./db/mysql:/docker-entrypoint-initdb.d/:ro

  traefik:
    image: traefik:v2.9
    command: --api.insecure=true --providers.docker
    ports:
      - 80:80
      - 8080:8080
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

With this configuration I am getting an error:

error while parsing rule Host(localhost) && PathRegexp(^/api/.*): unsupported function: PathRegexp

2 Answers 2

0

PathRegexp does not exist (anymore). You can use Path or PathPrefix (see: https://doc.traefik.io/traefik/routing/routers/#rule)

You can use

traefik.http.routers.api.rule=Host(`localhost`) && PathPrefix(`/api/`)

This will send /api to the UI container (but /api/ goes to the other container).

If you need /api/(trailing slash) to go to the UI container, you can use a regex like so:

traefik.http.routers.api.rule=Host(`localhost`) && Path(`/api/{regex:.+}`)

Note that regex here is just the name of the match group, it can be changed to anything else. .+ makes sure that it only matches if there is at least one character after /api/.

In general, the longest rules have the highest priority.

Sign up to request clarification or add additional context in comments.

3 Comments

thx, and how can i resolve my issue where i need the path /api/ to be redirected to UI container while everything after path /api/ (for example /api/test or /api/another-test) needs to be redirected to API container?
If PathRegexp doesn’t exist anymore, why is it in the documentation? doc.traefik.io/traefik/routing/routers/…
I’m a dummy. This person is using traefik v2.9. Disregard.
0

The PathRegexp function is supported as of Traefik v3.1.0 (commit).

So if you got an unsupported function: PathRegexp error, chances are you are using Traefik v2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.