英文:
Running multiple services with traefik docker on one domain, applies wrong routes
问题
我正在尝试运行两个服务,使用一个域名,但是Traefik会将/api
请求重定向到接口服务,即使我添加了PathPrefix
,当我尝试访问API时,接口服务记录了404代码。
以下是我的Docker Compose文件:
services:
traefik:
image: "traefik:latest"
command:
- --log.level=ERROR
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --accesslog.format=json
- --entrypoints.web.address=:80
- --entrypoints.websecure.address=:443
- --entrypoints.web.http.redirections.entryPoint.to=websecure
- --entrypoints.web.http.redirections.entryPoint.scheme=https
- --entrypoints.web.http.redirections.entrypoint.permanent=true
- --certificatesresolvers.my-resolver.acme.tlschallenge=true
- --certificatesresolvers.my-resolver.acme.email=support@example.com
- --certificatesresolvers.my-resolver.acme.storage=/letsencrypt/acme.json
ports:
- "80:80"
- "8080:8080"
- "443:443"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- ./certs:/letsencrypt
depends_on:
- api_server
interface:
labels:
- "traefik.enable=true"
- "traefik.http.routers.interface.rule=Host(`${DOMAIN}`)"
- "traefik.http.routers.interface.entrypoints=websecure"
- "traefik.http.routers.interface.tls.certresolver=my-resolver"
- "traefik.http.services.interface.loadbalancer.server.port=80"
api_server:
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.rule=Host(`${DOMAIN}`) && PathPrefix(`/api`)"
- "traefik.http.routers.api.entrypoints=websecure"
- "traefik.http.routers.api.tls.certresolver=my-resolver"
- "traefik.http.services.api.loadbalancer.server.port=8000"
- "traefik.http.routers.api.service=api"
- "traefik.http.routers.api.middlewares=api_strip"
- "traefik.http.middlewares.api_strip.stripprefix.prefixes=/api"
- "traefik.http.middlewares.api_strip.stripprefix.forceslash=false"
英文:
I'm trying to run two services, using one domain, but traefik redirects /api
requests to interface service, even if I added PathPrefix
, interface service logs 404 code when im trying to access api.
Here is my docker compose file:
services:
traefik:
image: "traefik:latest"
command:
- --log.level=ERROR
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --accesslog.format=json
- --entrypoints.web.address=:80
- --entrypoints.websecure.address=:443
- --entrypoints.web.http.redirections.entryPoint.to=websecure
- --entrypoints.web.http.redirections.entryPoint.scheme=https
- --entrypoints.web.http.redirections.entrypoint.permanent=true
- --certificatesresolvers.my-resolver.acme.tlschallenge=true
- --certificatesresolvers.my-resolver.acme.email=support@example.com
- --certificatesresolvers.my-resolver.acme.storage=/letsencrypt/acme.json
ports:
- "80:80"
- "8080:8080"
- "443:443"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- ./certs:/letsencrypt
depends_on:
- api_server
interface:
labels:
- "traefik.enable=true"
- "traefik.http.routers.interface.rule=Host(`${DOMAIN}`)"
- "traefik.http.routers.interface.entrypoints=websecure"
- "traefik.http.routers.interface.tls.certresolver=my-resolver"
- "traefik.http.services.interface.loadbalancer.server.port=80"
api_server:
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.rule=Host(`${DOMAIN}`) && PathPrefix(`/api`)"
- "traefik.http.routers.api.entrypoints=websecure"
- "traefik.http.routers.api.tls.certresolver=my-resolver"
- "traefik.http.services.api.loadbalancer.server.port=8000"
- "traefik.http.routers.api.service=api"
- "traefik.http.routers.api.middlewares=api_strip"
- "traefik.http.middlewares.api_strip.stripprefix.prefixes=/api"
- "traefik.http.middlewares.api_strip.stripprefix.forceslash=false"
答案1
得分: 0
在将问题重新发布到traefik labs之后,我得到了答案。我为每个服务添加了主机名,然后它就起作用了:
services:
interface:
hostname: interface
labels:
api_server:
hostname: api_server
labels:
英文:
After reposting question to traefik labs, I got the answer. I added hostname to each service and it worked:
services:
interface:
hostname: interface
labels:
api_server:
hostname: api_server
labels:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论