Search code examples
dockerdocker-composetraefik

Redirect URL to another URL by regex with Traefik


I want to redirect one URL to another URL through traefik.

I am using docker for this configuration and below is my docker-compose.yml file

version: '3'

services:
  reverse-proxy:
    # The official v2.0 Traefik docker image
    image: containous/whoami
    container_name: "whoami_cont"
    # Enables the web UI and tells Traefik to listen to docker
    ports:
      # The HTTP port
      - 80:80
      # The Web UI (enabled by --api.insecure=true)
      - 8080:8080
    labels:
      - traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]
      - traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=http://localhost:8081/4

then I am running docker-compose command successfully. Then I am going to hit any URL which matching regex pattern but URL is not redirecting to another URL - http://localhost:8081/4 as per configuration. I am using traefik version 2.0

Please let me know if any configuration is missing.


Solution

  • Following official example, it seems that your configuration is missing an actual Traefik instance:

      traefik:
        image: "traefik:v2.0.0-rc3"
        container_name: "traefik"
        command:
          - "--log.level=DEBUG"
          - "--api.insecure=true"
          - "--providers.docker=true"
          - "--providers.docker.exposedbydefault=false"
          - "--entrypoints.web.address=:80"
        ports:
          - "80:80"
          - "8080:8080"
        volumes:
          - "/var/run/docker.sock:/var/run/docker.sock:ro"
    

    After adding Traefik to your docker-compose file and defining middleware, you should attach your middleware to a router:

          - "traefik.http.routers.whoami.middlewares=test-replacepathregex"
          - "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
          - "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$$1"
    

    The full and working (but simplified) example of replacing /foo/123 by /bar/123:

    version: "3.3"
    
    services:
      traefik:
        image: "traefik:v2.0.0-rc3"
        container_name: "traefik"
        command:
          - "--log.level=DEBUG"
          - "--api.insecure=true"
          - "--providers.docker=true"
          - "--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: "simple-service"
        labels:
          - "traefik.enable=true"
          - "traefik.http.routers.whoami.rule=Host(`localhost`)"
          - "traefik.http.routers.whoami.entrypoints=web"
          - "traefik.http.routers.whoami.middlewares=test-replacepathregex"
          - "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
          - "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$$1"