Traefik: Level=error msg=“未找到字段,节点:我的网站” providerName=docker

Traefik: Level=error msg=“field not found, node: mywebsite” providerName=docker

我正在构建一个静态网站,使用 Gatsby, and I am using Nginx 来提供静态文件。

我还在 Docker 容器中设置 Docker for the application deployment to production and also using Traefik 作为反向代理。

Traefik runs on a different container while the Gatsby application runs on a different container with Nginx在一起。

但是,当我 运行 生产中的应用程序时,出现此错误:

level=error msg="field not found, node: mywebsite" providerName=docker container=web-my-website

这是我的代码:

Nginx 的defualt.conf

server {
  listen 3008;
  add_header Cache-Control no-cache;
  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
    expires -1;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

Docker文件

# Set base image
FROM node:latest AS builder

# Set working directory
WORKDIR /app

# Copy package.json and install packages
COPY package.json .
RUN npm install

# Copy other project files and build
COPY . ./
RUN npm run build

# Set nginx image
FROM nginx:latest

# Nginx config
RUN rm -rf /etc/nginx/conf.d/default.conf
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf

# Static build
COPY --from=builder /app/public /usr/share/nginx/html

# Set working directory
WORKDIR /usr/share/nginx/html

# Start Nginx server
CMD ["/bin/bash", "-c", "nginx -g \"daemon off;\""]

Gatsby 应用程序的 docker-compose.yml

version: "3"

services:
  web:
    image: my-website
    build:
      context: .
      dockerfile: Dockerfile
    expose:
      - "3004"
    labels:
      - traefik.enable=true
      - traefik.http.routers.mywebsite.rule=Host(`mywebsite.com`)
      - traefik.http.services.educollectwebsite.loadbalancer.server.port=3004
    restart: always
    volumes:
      - .:/app
networks:
  default:
    external:
      name: traefik-proxy

Traefik 的 docker-compose.yml

version: "3"

services:
  reverse-proxy:
    # The official v2 Traefik docker image
    image: traefik:v2.2
    # Enables the web UI and tells Traefik to listen to docker
    command:
      - --api.insecure=true
      - --entrypoints.web.address=:80
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
    ports:
      # The HTTP port
      - "88:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8088:8080"
    restart: always
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock

networks:
  default:
    external:
      name: traefik-proxy

我似乎无法弄清楚这里的问题是什么。我们将不胜感激任何形式的帮助。

在与我的直线经理一起工作了几个小时后,我终于能够解决它。

问题是我在 Nginx default.conf 文件中定义了端口 3008,然后在 [=45] 中定义了端口 3004 =]Gatsby 应用程序的 docker-compose.yml 文件。这不允许流量从 Traefik 反向代理进入应用程序。因为两个端口不同。

解决方案 1:

Nginx default.confGatsby 应用程序的 docker- 中简单地定义 3008 的相同端口compose.yml 文件修复了它:

Nginx 的defualt.conf

server {
  listen 3008;
  add_header Cache-Control no-cache;
  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
    expires -1;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

Gatsby 应用程序的 docker-compose.yml

version: "3"

services:
  web:
    image: my-website
    build:
      context: .
      dockerfile: Dockerfile
    expose:
      - "3004"
    labels:
      - traefik.enable=true
      - traefik.http.routers.mywebsite.rule=Host(`mywebsite.com`)
      - traefik.http.services.educollectwebsite.loadbalancer.server.port=3008
    restart: always
    volumes:
      - .:/app
networks:
  default:
    external:
      name: traefik-proxy

方案二:

在 Traefik 中定义默认端口,在 Nginx default.confGatsby 应用程序的 [=83] 中是端口 80 =]-compose.yml 文件修复了它。这在部署静态应用程序时更可取,因为它可以帮助我为应用程序假设一个合理的默认值。

Nginx 的defualt.conf

server {
  listen 80;
  add_header Cache-Control no-cache;
  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
    expires -1;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

Gatsby 应用程序的 docker-compose.yml

version: "3"

services:
  web:
    image: my-website
    build:
      context: .
      dockerfile: Dockerfile
    expose:
      - "80"
    labels:
      - traefik.enable=true
      - traefik.http.routers.mywebsite.rule=Host(`mywebsite.com`)
    restart: always
    volumes:
      - .:/app
networks:
  default:
    external:
      name: traefik-proxy

注意:在应用程序中使用与 Traefik 相同的端口,即端口 80,则不需要 Traefik 负载均衡器服务。

- traefik.http.services.educollectwebsite.loadbalancer.server.port=80

就这些了。

希望对您有所帮助