问题
I am using traefik version 2(or 2.x) and I want to forward all the request from port 80 to different port like 8081 with traefik router. So request like http://localhost/xx will be forwarded to http://localhost:8081/xx URL.
I am newbie with traefik and I am using docker for this configuration. Below is my docker-compose.yml file configuration. After configuring this traefik dashboard is loaded on http://localhost:8080/dashboard/#/ URL but request forwarding is not worked.
version: "3"
services:
traefik:
image: "traefik:v2.1.0"
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.entrypoints=web"
- "traefik.http.services.whoami.loadbalancer.server.port=8081"
- "traefik.docker.network=proxy"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
Any help on this would be appreciated.
回答1:
you need to map your service port to 8081
this is a fully working/tested example where you can access whoami
by going to http://whoami.docker.local:8081
or http://whoami.docker.local
version: "3"
services:
traefik:
image: traefik
command:
- --api.insecure=true
- --providers.docker=true
ports:
- "80:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
labels:
- traefik.http.routers.api.rule=Host(`traefik.docker.local`)
- traefik.http.routers.api.service=api@internal
whoami:
image: containous/whoami
ports:
- "8081:80"
labels:
- traefik.http.routers.whoami.rule=Host(`whoami.docker.local`)
- traefik.http.routers.whoami.service=whoami@docker
- traefik.http.services.whoami.loadbalancer.server.port=80
it works on port 80
and also 8081
, as per your request.
root@d:~# lsof -i :80,8081
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 28208 root 4u IPv6 51666675 0t0 TCP *:tproxy (LISTEN)
docker-pr 28265 root 4u IPv6 51671715 0t0 TCP *:http (LISTEN)
but it could be easier to help if you explain why you want to access :8081
,
because traefik
is used so we dont have to do those kind of redirections.
来源:https://stackoverflow.com/questions/59782442/request-forwarding-on-specific-port-with-traefikv2-router