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.
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"