Traefik v2:将 http:///servername/foo 重定向到 http://servername:port
Traefik v2: redirect http://servername/foo to http://servername:port
我的家庭网络中的 raspberrypi 上有 traefik(2.3.5 版)和其他服务 运行。每个服务在同一台机器上的不同端口上 运行。
我希望 traefik 将调用从子路径重定向到不同的 url。像这样:
http:///myServer/omv --> http://myServer:8000
http:///myServer/heimdall --> http://myServer:8003
但是我无法让它工作。我的路由器没有提供 domain/subdomains 所以我不得不使用子路径。
OMV 在本地安装,因此我创建了一个文件提供程序,这是唯一可用的文件提供程序。我无法找出我的 docker-compose 文件有什么问题,例如海姆达尔
这是 traefik docker-compose.yml:
version: '2'
services:
traefik:
image: traefik:latest
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
ports:
- 80:80
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- /root/myDockers/traefik/data/traefik.yml:/traefik.yml:ro
- /root/myDockers/traefik/data/non-docker-services.yml:/non-docker-services.yml:ro
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=http"
- "traefik.http.routers.traefik.rule=PathPrefix(`/api`) || PathPrefix(`/traefik`)"
- "traefik.http.middlewares.traefik-auth.basicauth.users=XXXX:YYYYYYYYYYYYYYYYY"
- "traefik.http.routers.traefik.middlewares=traefik-auth,traefik-strip-prefix"
- "traefik.http.middlewares.traefik-strip-prefix.stripprefix.prefixes=/traefik"
- "traefik.http.routers.traefik.service=api@internal"
Traefik.yml:
api:
dashboard: true
# insecure: true
debug: true
log:
format: json
level: DEBUG
accessLog:
format: common
entryPoints:
http:
address: ":80"
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
watch: true
file:
filename: /non-docker-services.yml
watch: true
非docker-services.yml:
http:
routers:
omv:
entryPoints:
- "http"
rule: "PathPrefix(`/omv`)"
service: omv
middlewares:
- omv-rr
middlewares:
omv-rr:
redirectRegex:
regex: "^http://myServer/omv(.*)"
replacement: "http://myServer:8000"
permanent: true
services:
omv:
loadBalancer:
servers:
- url: "http://myServer:8000"
passHostHeader: true
海姆达尔docker-compose.yml:
version: "2"
services:
heimdall:
image: ghcr.io/linuxserver/heimdall
container_name: heimdall
environment:
- PUID=1000
- PGID=1000
- TZ=Europe/Berlin
volumes:
- /root/myDockers/heimdall/appdata/config:/config
ports:
- 8003:80
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.hppt.routers.heimdall.entrypoints=http"
- "traefik.http.routers.heimdall.rule=PathPrefix(`/heimdall`)"
- "traefik.http.routers.heimdall.middlewares=heimdall-redirect"
- "traefik.http.middlewares.heimdall-redirect.redirectregex.regex=`^http://myServer/heimdall(.*)`"
- "traefik.http.middlewares.heimdall-redirect.redirectregex.replacement=`http://myServer:8003`"
- "traefik.http.middlewares.heimdall-redirect.redirectregex.permanent=true"
- "traefik.http.services.heimdall.loadbalancer.server.url=`http://myServer:8003`"
当我调用 http:///myServer/heimdall 时没有任何反应,一段时间后我超时了。我认为原因是,traefik 正试图转发
到 heimdall 172.31.0.2:80 的 docker 内部网络地址,当然我的客户端无法访问该地址。在 traefik 日志中它指出:
特雷菲克 | {"entryPointName":"http","level":"debug","msg":"正在创建服务器 0 http://172.31.0.2:80","routerName":"heimdall@docker","serverName":0,"serviceName":"heimdall","time":"2020-12-13T21:55:02+01:00"}
谁能给我指出正确的方向?
你能勾选“traefik.http.routers.heimdall.rule”的“主机”吗
也许需要这样,
"traefik.http.routers.heimdall.rule=Host(`myServer`) && PathPrefix(`/heimdall`)"
经过进一步 google 的工作,我想我在这个论坛 post 中找到了答案:https://community.traefik.io/t/traefik-v2-1-4-how-to-create-a-static-route-and-redirect-to-a-specific-host-and-port/4637
他们说的是,如果您使用 docker 标签来配置 traefik,则只能路由到 docker 容器本身。对于 docker 容器之外的路由,您需要使用 docker 文件提供程序。
这也解释了为什么它对我适用于 OMV 但不适用于 heimdall。
我的家庭网络中的 raspberrypi 上有 traefik(2.3.5 版)和其他服务 运行。每个服务在同一台机器上的不同端口上 运行。 我希望 traefik 将调用从子路径重定向到不同的 url。像这样:
http:///myServer/omv --> http://myServer:8000
http:///myServer/heimdall --> http://myServer:8003
但是我无法让它工作。我的路由器没有提供 domain/subdomains 所以我不得不使用子路径。
OMV 在本地安装,因此我创建了一个文件提供程序,这是唯一可用的文件提供程序。我无法找出我的 docker-compose 文件有什么问题,例如海姆达尔
这是 traefik docker-compose.yml:
version: '2'
services:
traefik:
image: traefik:latest
container_name: traefik
restart: unless-stopped
security_opt:
- no-new-privileges:true
ports:
- 80:80
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- /root/myDockers/traefik/data/traefik.yml:/traefik.yml:ro
- /root/myDockers/traefik/data/non-docker-services.yml:/non-docker-services.yml:ro
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.entrypoints=http"
- "traefik.http.routers.traefik.rule=PathPrefix(`/api`) || PathPrefix(`/traefik`)"
- "traefik.http.middlewares.traefik-auth.basicauth.users=XXXX:YYYYYYYYYYYYYYYYY"
- "traefik.http.routers.traefik.middlewares=traefik-auth,traefik-strip-prefix"
- "traefik.http.middlewares.traefik-strip-prefix.stripprefix.prefixes=/traefik"
- "traefik.http.routers.traefik.service=api@internal"
Traefik.yml:
api:
dashboard: true
# insecure: true
debug: true
log:
format: json
level: DEBUG
accessLog:
format: common
entryPoints:
http:
address: ":80"
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
watch: true
file:
filename: /non-docker-services.yml
watch: true
非docker-services.yml:
http:
routers:
omv:
entryPoints:
- "http"
rule: "PathPrefix(`/omv`)"
service: omv
middlewares:
- omv-rr
middlewares:
omv-rr:
redirectRegex:
regex: "^http://myServer/omv(.*)"
replacement: "http://myServer:8000"
permanent: true
services:
omv:
loadBalancer:
servers:
- url: "http://myServer:8000"
passHostHeader: true
海姆达尔docker-compose.yml:
version: "2"
services:
heimdall:
image: ghcr.io/linuxserver/heimdall
container_name: heimdall
environment:
- PUID=1000
- PGID=1000
- TZ=Europe/Berlin
volumes:
- /root/myDockers/heimdall/appdata/config:/config
ports:
- 8003:80
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.hppt.routers.heimdall.entrypoints=http"
- "traefik.http.routers.heimdall.rule=PathPrefix(`/heimdall`)"
- "traefik.http.routers.heimdall.middlewares=heimdall-redirect"
- "traefik.http.middlewares.heimdall-redirect.redirectregex.regex=`^http://myServer/heimdall(.*)`"
- "traefik.http.middlewares.heimdall-redirect.redirectregex.replacement=`http://myServer:8003`"
- "traefik.http.middlewares.heimdall-redirect.redirectregex.permanent=true"
- "traefik.http.services.heimdall.loadbalancer.server.url=`http://myServer:8003`"
当我调用 http:///myServer/heimdall 时没有任何反应,一段时间后我超时了。我认为原因是,traefik 正试图转发 到 heimdall 172.31.0.2:80 的 docker 内部网络地址,当然我的客户端无法访问该地址。在 traefik 日志中它指出: 特雷菲克 | {"entryPointName":"http","level":"debug","msg":"正在创建服务器 0 http://172.31.0.2:80","routerName":"heimdall@docker","serverName":0,"serviceName":"heimdall","time":"2020-12-13T21:55:02+01:00"}
谁能给我指出正确的方向?
你能勾选“traefik.http.routers.heimdall.rule”的“主机”吗
也许需要这样,
"traefik.http.routers.heimdall.rule=Host(`myServer`) && PathPrefix(`/heimdall`)"
经过进一步 google 的工作,我想我在这个论坛 post 中找到了答案:https://community.traefik.io/t/traefik-v2-1-4-how-to-create-a-static-route-and-redirect-to-a-specific-host-and-port/4637
他们说的是,如果您使用 docker 标签来配置 traefik,则只能路由到 docker 容器本身。对于 docker 容器之外的路由,您需要使用 docker 文件提供程序。 这也解释了为什么它对我适用于 OMV 但不适用于 heimdall。