6

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.

Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
Dhaval Goti
  • 447
  • 2
  • 10
  • 25

2 Answers2

8

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"
Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
  • Thanks Yury for this. It builds successfully but when I hit the URL, it is not redirecting to replacement URL. I can see the output of the whoami service as shown in official example but URL redirection is not happening. – Dhaval Goti Jan 08 '20 at 13:48
  • 2
    Because it is not redirect, it's a replacement: you see the same value in browser, but the backend sees another value (look at `whoami` reponse). If you need to redirect, use redirect middleware which is similar to replacepath middleware. – Yury Fedorov Jan 08 '20 at 13:50
  • Thanks Yury. whoami service is responding perfect for replacement but when I am using redirection middleware, browser URL is not redirecting and 404 page not found displays. Any idea for this? – Dhaval Goti Jan 08 '20 at 14:14
  • 3
    Most likely you misconfigured the middleware and need to look into it. Anyway, the original issue in the question is that you didn't actually attach the middleware to a router. Now when you do attach it, you can start debugging the next issue. – Yury Fedorov Jan 08 '20 at 15:22
  • Yes Yury, you are correct about attach the middleware to a router. I am missing this part. Now I am able to redirect it successfully with your provided example. Thanks for your kind help. – Dhaval Goti Jan 09 '20 at 06:15
  • Hello. What if I wanted to replace not just the path but the entire URL? Say if this replace regex is detected, /v1/auth/verify, I wanted to forward this to another service, http://auth.docker.localhost ? – Karl Anthony Baluyot Dec 27 '20 at 12:37
2

It's pretty straightforward if you are using an Ingressroute. Let say I want to redirect from example.com to app.example.com using traefik

First thing would be to create a Middleware :

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: example-redirectregex
spec:
  redirectRegex:
    regex: ^https://example.com/(.*)
    replacement: https://app.example.com/${1}

If you already have an Ingressroute then you can add an additional rule that will point example.com to the same backend service used by app.example.com and use the middleware we created above

- kind: Rule
    match: Host(`example.com`)
    middlewares:
    - name: example-redirectregex
      namespace: traefik
    services:
    - kind: Service
      name: master-prod
      namespace: app-web
      port: 80

So, finally your Ingressroute should look something like the below:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  generation: 3
  labels:
    app: app-web
    product: example.com
  name: example-com
  namespace: traefik
spec:
  entryPoints:
  - websecure
  routes:
  - kind: Rule
    match: Host(`example.com`)
    middlewares:
    - name: example-redirectregex
      namespace: traefik
    services:
    - kind: Service
      name: example
      namespace: app-web
      port: 80
  - kind: Rule
    match: Host(`app.example.com`)
    services:
    - kind: Service
      name: example
      namespace: app-web
      port: 80
  tls:
    secretName: cert-example-com

Now, if you browse example.com it should redirect you to app.example.com