I am pretty new to Traefik so apologies upfront if this is too simple.
I have gone through the Traefik documentation as well as this communities posts, but could not find an answer to what I am looking for.
My setup is as follows:
Container 1 runs WebApp1, listens on port 80 default path of web application is /
Container 2 runs WebApp2, listens on port 80 default path of web application is /
I want to use Traefik to route requests to each web app using different paths (that don't necessarily exist on the web applications)
http://host.domain/webapp1 requests go to Container 1. I would need to strip the prefix since the web application is not hosted at /webapp1 http://host.domain/webapp2 requests go to Container 2. Same strip would have to be done here as well.
I have tried the addpath and strippath middlewares, but it did not work like I expected. Can Traefik handle this type of flow?
Here is the sample config I tried that did not work:
traefik-reverse-proxy:
# The official v2 Traefik docker image
image: traefik:v2.1
# Enables the web UI and tells Traefik to listen to docker
command:
# Enabled Traefik logging
- "--accesslog=true"
- "--log=true"
- "--log.level=debug"
# Traefik will listen on port 8080 by default for API request.
- "--api.insecure=true"
# Enabling docker provider
- "--providers.docker=true"
# Do not expose containers unless explicitly told so
- "--providers.docker.exposedbydefault=false"
# Traefik will listen to incoming request on entry point named web on port 80 (HTTP)
- "--entrypoints.web.address=:80"
# Traefik will listen to incoming request on entry point named websecure on port 443 (HTTPS)
- "--entrypoints.websecure.address=:443"
volumes:
# So that Traefik can listen to the Docker events
- "/var/run/docker.sock:/var/run/docker.sock:ro"
ports:
# The HTTP port
- "80:80"
# The HTTPS port
- "443:443"
# The Web UI (enabled by --api.insecure=true)
- "8080:8080"
web-app1:
image: "some-image"
container_name: "web-app1"
labels:
- "traefik.enable=true"
# Define router for web-app1
- "traefik.http.routers.web-app1-router.rule=Host(`hostname.domain`)&&Path(`/web-app1`)"
- "traefik.http.routers.web-app1-router.entrypoints=web,websecure"
- "traefik.http.routers.web-app1-router.tls=true"
- "traefik.http.services.web-app1-router.loadbalancer.server.port=80"
# Define middleware for web-app1
- "traefik.http.middlewares.web-app1-strip-prefix.stripprefix.prefixes=/web-app1"
# Apply middleware to router
- "traefik.http.routers.web-app1-router.middlewares=web-app1-strip-prefix@docker"
web-app2:
image: "some-image"
container_name: "web-app2"
labels:
- "traefik.enable=true"
# Define router for web-app2
- "traefik.http.routers.web-app2-router.rule=Host(`hostname.domain`)&&Path(`/web-app2`)"
- "traefik.http.routers.web-app2-router.entrypoints=web,websecure"
- "traefik.http.routers.web-app2-router.tls=true"
- "traefik.http.services.web-app2-router.loadbalancer.server.port=80"
# Define middleware for web-app2
- "traefik.http.middlewares.web-app2-strip-prefix.stripprefix.prefixes=/web-app2"
# Apply middleware to router
- "traefik.http.routers.web-app2-router.middlewares=web-app2-strip-prefix@docker"
I was able to get this to work by using the hostname (using subdomains) as the decision point for selecting the service, but I would like to avoid this if possible: