在一个域名上使用Traefik Docker运行多个服务时,应用了错误的路由。

huangapple go评论109阅读模式
英文:

Running multiple services with traefik docker on one domain, applies wrong routes

问题

我正在尝试运行两个服务,使用一个域名,但是traefik将/api请求重定向到接口服务,即使我添加了PathPrefix,当我尝试访问api时,接口服务记录了404错误代码。

这是我的Docker Compose文件:

  1. services:
  2. traefik:
  3. image: "traefik:latest"
  4. command:
  5. - --log.level=ERROR
  6. - --providers.docker=true
  7. - --providers.docker.exposedbydefault=false
  8. - --accesslog.format=json
  9. - --entrypoints.web.address=:80
  10. - --entrypoints.websecure.address=:443
  11. - --entrypoints.web.http.redirections.entryPoint.to=websecure
  12. - --entrypoints.web.http.redirections.entryPoint.scheme=https
  13. - --entrypoints.web.http.redirections.entrypoint.permanent=true
  14. - --certificatesresolvers.my-resolver.acme.tlschallenge=true
  15. - --certificatesresolvers.my-resolver.acme.email=support@example.com
  16. - --certificatesresolvers.my-resolver.acme.storage=/letsencrypt/acme.json
  17. ports:
  18. - "80:80"
  19. - "8080:8080"
  20. - "443:443"
  21. volumes:
  22. - "/var/run/docker.sock:/var/run/docker.sock:ro"
  23. - ./certs:/letsencrypt
  24. depends_on:
  25. - api_server
  26. interface:
  27. labels:
  28. - "traefik.enable=true"
  29. - "traefik.http.routers.interface.rule=Host(`${DOMAIN}`)"
  30. - "traefik.http.routers.interface.entrypoints=websecure"
  31. - "traefik.http.routers.interface.tls.certresolver=my-resolver"
  32. - "traefik.http.services.interface.loadbalancer.server.port=80"
  33. api_server:
  34. labels:
  35. - "traefik.enable=true"
  36. - "traefik.http.routers.api.rule=Host(`${DOMAIN}`) && PathPrefix(`/api`)"
  37. - "traefik.http.routers.api.entrypoints=websecure"
  38. - "traefik.http.routers.api.tls.certresolver=my-resolver"
  39. - "traefik.http.services.api.loadbalancer.server.port=8000"
  40. - "traefik.http.routers.api.service=api"
  41. - "traefik.http.routers.api.middlewares=api_strip"
  42. - "traefik.http.middlewares.api_strip.stripprefix.prefixes=/api"
  43. - "traefik.http.middlewares.api_strip.stripprefix.forceslash=false"

请注意,这只是你提供的Docker Compose文件的翻译,我无法提供关于Traefik配置的技术支持。

英文:

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:

  1. services:
  2. traefik:
  3. image: "traefik:latest"
  4. command:
  5. - --log.level=ERROR
  6. - --providers.docker=true
  7. - --providers.docker.exposedbydefault=false
  8. - --accesslog.format=json
  9. - --entrypoints.web.address=:80
  10. - --entrypoints.websecure.address=:443
  11. - --entrypoints.web.http.redirections.entryPoint.to=websecure
  12. - --entrypoints.web.http.redirections.entryPoint.scheme=https
  13. - --entrypoints.web.http.redirections.entrypoint.permanent=true
  14. - --certificatesresolvers.my-resolver.acme.tlschallenge=true
  15. - --certificatesresolvers.my-resolver.acme.email=support@example.com
  16. - --certificatesresolvers.my-resolver.acme.storage=/letsencrypt/acme.json
  17. ports:
  18. - "80:80"
  19. - "8080:8080"
  20. - "443:443"
  21. volumes:
  22. - "/var/run/docker.sock:/var/run/docker.sock:ro"
  23. - ./certs:/letsencrypt
  24. depends_on:
  25. - api_server
  26. interface:
  27. labels:
  28. - "traefik.enable=true"
  29. - "traefik.http.routers.interface.rule=Host(`${DOMAIN}`)"
  30. - "traefik.http.routers.interface.entrypoints=websecure"
  31. - "traefik.http.routers.interface.tls.certresolver=my-resolver"
  32. - "traefik.http.services.interface.loadbalancer.server.port=80"
  33. api_server:
  34. labels:
  35. - "traefik.enable=true"
  36. - "traefik.http.routers.api.rule=Host(`${DOMAIN}`) && PathPrefix(`/api`)"
  37. - "traefik.http.routers.api.entrypoints=websecure"
  38. - "traefik.http.routers.api.tls.certresolver=my-resolver"
  39. - "traefik.http.services.api.loadbalancer.server.port=8000"
  40. - "traefik.http.routers.api.service=api"
  41. - "traefik.http.routers.api.middlewares=api_strip"
  42. - "traefik.http.middlewares.api_strip.stripprefix.prefixes=/api"
  43. - "traefik.http.middlewares.api_strip.stripprefix.forceslash=false"

答案1

得分: 0

在将问题转发给traefik labs后,我得到了答案。我为每个服务添加了主机名,然后它就起作用了:

  1. services:
  2. interface:
  3. hostname: interface
  4. labels:
  5. api_server:
  6. hostname: api_server
  7. labels:
英文:

After reposting question to traefik labs, I got the answer. I added hostname to each service and it worked:

  1. services:
  2. interface:
  3. hostname: interface
  4. labels:
  5. api_server:
  6. hostname: api_server
  7. labels:

huangapple
  • 本文由 发表于 2023年8月9日 08:46:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定