为什么我无法使用 Traefik 路由到我的 API 后端

Why am I unable to route to my API backend with Traefik

我有两个容器前端 (nginx:80) 和后端 (nodejs:3000)。 我正在尝试将所有路径重定向到我的前端:localhost/* 到我的前端 除了通往我的后端 API 的一条路径: localhost/v1/* 到我的后端

我通过只允许与我的后端通信来保护我的数据库容器 (mongodb)

这是我的docker-compose.yml(我只用这个)

version: '3'

services:
  traefik:
    image: traefik:v2.3
    container_name: traefik
    command:
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
    ports:
      - "8080:8080"
      - "443:443"
      - "80:80"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

  frontend:
    image: registry.gitlab.com/test/frontend
    container_name: frontend
    build:
      context: ../frontend/.
    labels:
      - traefik.enable=true
      - traefik.http.routers.frontend.rule=PathPrefix(`/`)
      - traefik.http.routers.frontend.entrypoints=web    
    networks:
      - traefik-network

  backend:
    image: registry.gitlab.com/test/backend
    container_name: backend
    build:
      context: ../backend/.
    labels:
      - traefik.enable=true
      - traefik.http.routers.backend.rule=PathPrefix(`/v1`)
      - traefik.http.routers.backend.service=backend
      - traefik.http.routers.backend.entrypoints=web
      - traefik.http.services.backend.loadbalancer.server.port=3000
    command: yarn start
    environment:
      - MONGODB_URL=mongodb://mongodb:27017/backend
    depends_on:
      - mongodb
    volumes:
      - ../backend/.:/usr/src/backend
    networks:
      - traefik-network
      - backend-network

  mongodb:
    image: mongo:4.2.1-bionic
    container_name: mongodb
    ports:
      - 27017:27017
    volumes:
      - dbdata:/data/db
    networks:
      - backend-network

volumes:
  dbdata:

networks:
  backend-network:
  traefik-network:

问题是...

如果前端(后端和 traefik 也是)打开

如果前端关闭但traefik和后端打开

我已经尝试了很多解决方案,但似乎都没有达到我想要的效果。 我误会了什么?

感谢您的帮助, 祝你有美好的一天

尝试将以下标签添加到后端服务

- "traefik.http.routers.backend.rule=Host(`servicex.me`) && Path(`/v1`)"

和前端

- traefik.http.routers.frontend.rule=Host(`servicex.me`)

您还需要将此行添加到您的 /etc/hosts

127.0.0.1 servicex.me

并确保停止和启动服务

完整示例

version: '3'

services:
  traefik:
    image: traefik:v2.3
    container_name: traefik
    command:
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
    ports:
      - "8080:8080"
      - "443:443"
      - "80:80"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

  frontend:
    image: registry.gitlab.com/test/frontend
    container_name: frontend
    build:
      context: ../frontend/.
    labels:
      - traefik.enable=true
      - traefik.http.routers.frontend.rule=Host(`servicex.me`)
      - traefik.http.routers.frontend.entrypoints=web
      - traefik.http.routers.frontend.service=frontend
      - traefik.http.services.frontend.loadbalancer.server.port=80
    networks:
      - traefik-network

  backend:
    image: registry.gitlab.com/test/backend
    container_name: backend
    build:
      context: ../backend/.
    labels:
      - traefik.enable=true
      - "traefik.http.routers.backend.rule=Host(`servicex.me`) && Path(`/v1`)"
      - traefik.http.routers.backend.service=backend
      - traefik.http.routers.backend.entrypoints=web
      - traefik.http.services.backend.loadbalancer.server.port=3000
    command: yarn start
    environment:
      - MONGODB_URL=mongodb://mongodb:27017/backend
    depends_on:
      - mongodb
    volumes:
      - ../backend/.:/usr/src/backend
    networks:
      - traefik-network
      - backend-network

  mongodb:
    image: mongo:4.2.1-bionic
    container_name: mongodb
    ports:
      - 27017:27017
    volumes:
      - dbdata:/data/db
    networks:
      - backend-network

volumes:
  dbdata:

networks:
  backend-network:
  traefik-network:

顺便说一句,为什么你同时需要traefik和nginx(两者都在做同样的工作),如果你能换一个就更好了。

我将此标签添加到我的容器中

traefik.docker.network=traefik-network

现在可以正常使用了