I am using Traefik in Docker Compose for request routing and trying to add the prefix /site1 to all paths using the addPrefix middleware. However, when accessing the homepage, I encounter an issue where the prefix is duplicated.
Expected behavior:
When visiting site1.${PROJECT_BASE_URL}/about, the request should be passed to the container as site1.${PROJECT_BASE_URL}/site1/about (this works correctly).
When visiting site1.${PROJECT_BASE_URL}/, the request should be passed to the container as site1.${PROJECT_BASE_URL}/site1.
Actual behavior (Bug):
When visiting site1.${PROJECT_BASE_URL}/about, it works fine and is handled as site1.${PROJECT_BASE_URL}/site1/about inside the container.
However, when visiting site1.${PROJECT_BASE_URL}/, it gets rewritten as site1.${PROJECT_BASE_URL}/site1/site1, causing a 404 error.
next:
# restart: always
image: wodby/node:16-dev-0.84.0
container_name: "${PROJECT_NAME}_next"
working_dir: /var/www/html
labels:
- "traefik.enable=true"
# Главный домен maindomen.ru
- "traefik.http.routers.${PROJECT_NAME}_gnext.rule=Host(`${PROJECT_BASE_URL}`)"
- "traefik.http.services.${PROJECT_NAME}_gnext.loadbalancer.server.port=5900"
- "traefik.http.routers.${PROJECT_NAME}_gnext.entrypoints=web"
# # Проксирование gz.maindomen.ru → maindomen.ru/gz
- "traefik.http.routers.site1.rule=Host(`site1.${PROJECT_BASE_URL}`)"
- "traefik.http.middlewares.add-prefix.addprefix.prefix=/user/2304"
- "traefik.http.routers.site1.middlewares=add-prefix@docker"
expose:
- "5900"
volumes:
- ./website:/var/www/html
command: sh -c 'yarn build && yarn dev'
I want to configure Traefik to redirect requests for certain URL paths (e.g., /site1/*) to a corresponding subdomain (site1.${PROJECT_BASE_URL})
Is my current Traefik configuration the right way to achieve this, or should I use a different approach to route specific URL paths to subdomains more efficiently?
PS
I found a configuration that works for me, but I'm not sure if this is the best approach. Here’s what I did:
# Middleware for the homepage (/)
- "traefik.http.middlewares.replace-root-home.replacepathregex.regex=^/$"
- "traefik.http.middlewares.replace-root-home.replacepathregex.replacement=/site1"
# Middleware for all other pages (/about, /contact, etc.)
- "traefik.http.middlewares.replace-root-all.replacepathregex.regex=^(.*)$"
- "traefik.http.middlewares.replace-root-all.replacepathregex.replacement=/site1$1"
# Apply different middleware for homepage and other pages
- "traefik.http.routers.site1-home.rule=Host(`site1.${PROJECT_BASE_URL}`) && Path(`/`)"
- "traefik.http.routers.site1-home.middlewares=replace-root-home@docker"
- "traefik.http.routers.site1.rule=Host(`site1.${PROJECT_BASE_URL}`)"
- "traefik.http.routers.site1.middlewares=replace-root-all@docker"
However, I’m not sure if this is the best way to achieve this in Traefik. Is there a more efficient or recommended way to do this?