【问题标题】:502 Bad Gateway error while opening a dockerized react app打开 dockerized react 应用程序时出现 502 Bad Gateway 错误
【发布时间】:2022-03-30 20:04:09
【问题描述】:

我在 React 中有一个使用 Nginx 作为服务器的多容器 docker 应用程序。当我尝试在浏览器中打开它时,我收到 502 Bad Gateway 错误消息:

2021/12/30 11:58:44 [error] 31#31: *31 connect() failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.23.0.2:3000/favicon.ico", host: "localhost:3050", referrer: "http://localhost:3050/"

这里是 nxing 配置文件:

upstream client {
  server client:3000;
}

upstream api {
  server api:5000;
}

server {
  listen 80;

  location / {
    proxy_pass http://client;
  }

  location /sockjs-node {
    proxy_pass http://client;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
  }

  location /api {
    rewrite /api/(.*) /$1 break;
    proxy_pass http://api;
  }
}

这是用于 Nginx 的 docker-compose 的片段:

nginx:
    depends_on:
      - api
      - client
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - '3050:80'

我该如何解决这个问题?

【问题讨论】:

  • 该错误的意思是:Nginx 尝试连接到client:3000(将其解析为 Docker 内部 IP 地址)并失败。 client 容器中正在运行什么? ports: 没关系; client 容器是否在运行 HTTP 服务器,正在监听 0.0.0.0:3000? (您在问题中显示的内容似乎按预期工作。)
  • 在客户端的 Dockerfile 中添加 EXPOSE 3000 有效,我可以访问该页面
  • 当我尝试发布值时,我得到了这个:2021/12/30 15:48:01 [error] 30#30: *20 connect() failed (113: No route to host) while connecting to upstream, client: 172.18.0.1, server: , request: "POST /api/values HTTP/1.1", upstream: "http://172.18.0.3:5000/values", host: "localhost:3050", referrer: "http://localhost:3050/" 它似乎试图去 '/values' 而不是 '/api/values'
  • 我猜应该是因为 nginx 配置,但是为什么会这样呢?
  • 没关系,现在可以使用了

标签: reactjs docker nginx


【解决方案1】:

感谢您发布这个问题,它帮助了我。 此外,在根目录内的 docker-compose.yml 文件中。我必须添加 stdin_open: true,

客户: 标准输入打开:真 建造: dockerfile: Dockerfile.dev 上下文:./client 卷: - /app/node_modules - ./client:/app

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 2019-08-10
    • 2018-01-02
    相关资源
    最近更新 更多