首页 > 解决方案 > Docker Compose 与 Express.js 和 Nginx - 502 响应

问题描述

我正在尝试使用 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;
    }
  }
}

标签: dockernginx

解决方案


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

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


推荐阅读