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

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

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:

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

发表评论

匿名网友

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

确定