【发布时间】:2020-05-22 02:37:10
【问题描述】:
尝试在 Traefik 中实现简单的 Nginx 请求路由。
不幸的是,Traefik 的 PathPrefix 无法开箱即用
location /app1/ {
proxy_bind $server_addr;
proxy_pass http://my-subdomain.localhost/app1/;
}
location /app2/ {
proxy_bind $server_addr;
proxy_pass http://my-subdomain.localhost/app2/;
}
location /app3/ {
proxy_bind $server_addr;
proxy_pass http://my-subdomain.localhost/app3/;
}
使用 docker-compose 旋转所有服务的 Traefik 配置代码。
文件:dockercompose.yml
version: '3.3'
services:
traefik-reverse-proxy:
container_name: traefik-reverse-proxy
image: traefik:v2.2
command:
- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
labels:
- "traefik.enable=true"
- "traefik.http.routers.reverse-proxy.rule=Host(`my-subdomain.localhost`) && PathPrefix(`/traefik`)"
- "traefik.http.routers.reverse-proxy.service=api@internal"
app1:
container_name: app1
image: registry.gitlab.com/my-hobby-project/app1:latest
labels:
- "traefik.enable=true"
- "traefik.http.routers.app1.rule=(Host(`my-subdomain.localhost`) && PathPrefix(`/app1`))"
app2:
container_name: app2
image: registry.gitlab.com/my-hobby-project/app2:latest
labels:
- "traefik.enable=true"
- "traefik.http.routers.app2.rule=(Host(`my-subdomain.localhost`) && PathPrefix(`/app2/`))"
app3:
container_name: app3
image: registry.gitlab.com/my-hobby-project/app3:latest
labels:
- "traefik.enable=true"
- "traefik.http.routers.app3.rule=(Host(`my-subdomain.localhost`) && PathPrefix(`/app3`))"
提示:
A. App3 是我的 Angular 应用程序,在这种情况下,我实际上可以访问 index.html 页面,但底层资源正在获取 404。
第一个请求:http://staging.localhost/app3 -> 找到 index.html。
以下请求:例如http://staging.localhost/assests/util.js -> 404(我希望traefik应该将此请求作为http://staging.localhost/app3/assests/util.js)
我也尝试使用AddPrefix,但没有成功。
添加前缀代码:
- "traefik.http.routers.app3.rule=Host(`staging.localhost`) && (PathPrefix(`/app3`))"
- "traefik.http.middlewares.add-app3.addprefix.prefix=/app3"
- "traefik.http.routers.app3.middlewares=add-app3"
B.还尝试在 pathPrefix 末尾使用斜杠(检查上面的 app2 示例代码),不幸的是,它也不起作用。
C.使用 Traefik 2.2 版(参见 docker-compose.yml 文件)
问题:
- 如何使用子域和路径前缀(即
http://my-subdomain.localhost/traefik)访问 traefik 仪表板- 如何访问 Angular 的底层资源:http://my-subdomain.localhost/app3`
【问题讨论】:
标签: docker docker-compose reverse-proxy traefik