Reverse proxy in Traefik with subdirectories.

Maciej
2 min readApr 1, 2020

This is an example of setting to reverse proxy the request received by Traefik to each Docker container, distribute requests to each container by looking at the sub-directory of the URL.

Docker Compose file

Explanation

The following is a rough explanation.

Processing flow

Trafik normally processes requests in the order of entry point-> router-> middleware-> service.

Constituent elements

  • Entry point: Entrance to Traefik. Set the address and port number. Here, only the port number is specified.
  • Router: Distributes requests to each service according to conditions.
  • Middleware: Edit the request (eg rewrite the path) before passing the request to the backend service.
  • Service: Backend service. Here is each Docker container.

Setting entry points

TRAEFIK_ENTRYPOINTS_FRONT_ADDRESS=:${FRONT_HTTP_PORT:-80}

The line above defines an entry point named front and specifies to listen on port 80. In addition, when defining with environment variables, write FRONT in uppercase, but it seems that it is treated as lowercase front on Traefik.

Router settings

- "traefik.http.routers.app1.entrypoints=front"

The above line defines the router with the name app1 and associates the entry point with the router.

- "traefik.http.routers.app1.rule=PathPrefix(`/app1{regex:$$|/.*}`)"

The above line defines the router with the name app1, and specifies that the request path should start with /app1, app1/, app1/**.
If it matches, it will be routed to the container where the rule is written.

Middleware settings

- "traefik.http.middlewares.app1-stripprefix.stripprefix.prefixes=/app1"

The above line defines a stripprefix middleware with the name app1-stripprefix and specifies that /app1 at the beginning of the request path be removed before making a request to the backend service.

Connection between router and middleware

- "traefik.http.routers.app1.middlewares=app1-stripprefix"

The above line specifies that router app1 and middleware app1-stripprefix are connected.

Operation check

Starting a container

Start the container.

$ cd /path/to/compose-file
$ docker-compose up --detach
$ docker-compose ps

Access via web browser

App1

Access http://192.168.123.123:80/app1 with a web browser and it will be OK if it is displayed as follows.

App2

Access http://192.168.123.123:80/app2 with a web browser and it will be OK if it is displayed as follows.

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.