简体   繁体   中英

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.

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM