0

I want to forward url with traefik(version 2) like If I hit URL http://localhost/1 then it is forwarded to http://localhost:8081/1.

I have tried with several configuration but no one works. Please find below configuration I have done but it didn't worked.

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"
      - "8081:8081"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  whoami:
    #image: "containous/whoami"
    #container_name: "simple-service"
    #command:
    #  - "--port=8081"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`localhost`)"
      - "traefik.http.routers.whoami.entrypoints=web"
      - "traefik.http.services.whoami.loadbalancer.server.port=8081" 
Dhaval Goti
  • 447
  • 2
  • 10
  • 25
  • So you want all network traffic arriving on `localhost:80` to be forwarded to the `whoami` service (or you want to forward it to `localhost:8081` regardless of what is running there)? – h3yduck Jan 11 '20 at 12:19
  • Actually not on whoami service but I want to forward to localhost:8081. – Dhaval Goti Jan 11 '20 at 12:55
  • 1
    Does this answer your question? [request forwarding on specific port with traefik(v2) router](https://stackoverflow.com/questions/59782442/request-forwarding-on-specific-port-with-traefikv2-router) – RASG Jan 20 '20 at 15:28

1 Answers1

0

I think you don't need to forward, since you have defined the loadbalancer port, add PathPrefix to your whoami routers rule should work. Like this

- "traefik.http.routers.whoami.rule=Host(`localhost`) && PathPrefix(`/1`)"

And if you do need forward requests, you can use middlewares.redirectregex.

- "traefik.http.routers.whoami.middlewares=whoami-redirectregex"
- "traefik.http.middlewares.whoami-redirectregex.redirectregex.regex=^http://localhost/(.*)"
- "traefik.http.middlewares.whoami-redirectregex.redirectregex.replacement=http://localhost:8081/$${1}"

FYI. middleware docs

呆胶布
  • 21
  • 2