【问题标题】:Docker Compose with Express.js and Nginx - 502 responseDocker Compose 与 Express.js 和 Nginx - 502 响应
【发布时间】:2019-08-21 11:23:56
【问题描述】:

我正在尝试使用 express.js 和 nginx 作为 Docker 的反向代理。 express 应用程序可以单独使用 docker,但是当我添加 nginx 时,我得到 502 响应。它打印这个:

nginx_1 | 2019/03/30 21:14:04 [error] 6#6: *3 connect() failed (111: Connection refused) while connection to upstream, client: 172.20.0.1, server: localhost, request: "GET / HTTP /1.1”,上游:“http://127.0.0.1:20000/”,主机:“localhost:21000”

docker-compose.yml

version: '3.3'

services:
  app:
    image: node:latest
    volumes:
      - ./app:/usr/src/service/app
    working_dir: /usr/src/service/app
    command: ["node", "app"]
    ports:
      - 20000:18000

  nginx: 
    image: nginx:latest
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/error.log:/etc/nginx/error_log.log
      - ./nginx/cache/:/etc/nginx/cache
    ports:
      - 21000:80

nginx.conf

events {

}

http {
  upstream upstream_server {
    server localhost:20000;
  }

  server {
    listen 127.0.0.1;
    listen 80;
    server_name localhost;

    location / {
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://upstream_server;
      break;
    }
  }
}

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

    您的 nginx 容器和应用程序容器在同一个网络上,但不在同一个主机上 - 因此localhost 不是您运行应用程序的地方。

    在上游尝试 app 作为 server 参数 - 这应该解析为 compose 网络中 app 容器的 IP。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2021-02-15
      • 2020-12-15
      • 1970-01-01
      • 2022-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多