Application URL write behind Traefik as Reverse Proxy possible?

Hi everybody,

I'm trying to setup Traefik 2.2.0 as Reverse Proxy for some containers. In detail, I'm running two web applications that I want to expose via dedicated directories /app1 and /app2.

I figured out how to set-up a middleware to strip /app2 from the URL before passing the request to the application. Otherwise the request results in a 404 error as GET / is replaced by GET /app2/.

Anyhow, the application has links on the landing page (e.g. /pageA, /pageB,...). Clicking on one of the links of course results in a 404 error as the URLs are not rewritten.

My question now is - how can I implement this URL rewriting?

Any hints or tips are welcome - I'm quite new to Traefik and reverse proxies in general. :slight_smile:

What I tried
I tried to assign redirectregex and replacepathregex middlewares to the application - with or without the stripprefix middleware:

    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.radio.rule=Host(`192.168.0.246`) && PathPrefix(`/app2`)"
      - "traefik.http.routers.radio.entrypoints=web"
      - "traefik.http.middlewares.radio_strip_prefix.stripprefix.prefixes=/app2"
      - "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/app2/(.*)"
      - "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/$$1"
      - "traefik.http.routers.radio.middlewares=radio_strip_prefix@docker,test-replacepathregex@docker"
      #- "traefik.http.routers.radio.middlewares=test-replacepathregex@docker"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.radio.rule=Host(`192.168.0.246`) && PathPrefix(`/app2`)"
      - "traefik.http.routers.radio.entrypoints=web"
      - "traefik.http.middlewares.radio_strip_prefix.stripprefix.prefixes=/app2"
      - "traefik.http.routers.radio.middlewares=radio_strip_prefix@docker,test-redirectregex@docker"
      - "traefik.http.routers.radio.middlewares=test-redirectregex@docker,replacepathregex@docker"
      #- "traefik.http.routers.radio.middlewares=test-redirectregex@docker"

None of them were working as expected.

Environment

  • Traefik 2.2.0 Docker Container
  • Docker CE 19.03 (armhf)
  • two Docker web applications

Dockerfile

version: "3.7"

services:
  traefik:
    image: traefik:2.2
    container_name: traefik
    command:
      - "--api.insecure=true"
      - "--providers.docker"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
  whoami:
    image: containous/whoami
    container_name: whoami
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`<ip-address>`) && PathPrefix(`/app1`)"
      - "traefik.http.routers.whoami.entrypoints=web"
  radio:
    image: stdevel/radio_api
    container_name: radio_dev
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.radio.rule=Host(`<ip-address>`) && PathPrefix(`/app2`)"
      - "traefik.http.routers.radio.entrypoints=web"
      - "traefik.http.middlewares.radio_strip_prefix.stripprefix.prefixes=/app2"
      ##- "traefik.http.routers.radio.middlewares=radio_strip_prefix@docker"
      #- "traefik.http.routers.radio.middlewares=radio_strip_prefix@docker,test-redirectregex@docker"
      #- "traefik.http.routers.radio.middlewares=test-redirectregex@docker"
      - "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/app2/(.*)"
      - "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/$$1"
      - "traefik.http.routers.radio.middlewares=radio_strip_prefix@docker,test-replacepathregex@docker"
1 Like

Hello

I am trying to do the same thing as you.

From what I have seen so far there is no way to do it only using Traefik.

Though some applications can be configured to run behind a reverse proxy using an url prefix, like discussed about Grafana for example : Why doesn't traefik readd PathPrefix prefixes to backend responses?

1 Like

Hey evdel,
that's interesting - thanks for sharing!

Best wishes,
Christian.

You're welcome. Finally I ended up creating one sub-domain for the other app I wanted to expose.

I think it is the simplest solution. Why not having something like app1.domain.com and app2.domain.com for example ?

Cheers