【问题标题】:Cannot connect proxy_server localhost:3000 to nginx config file (docker-compose project)无法将 proxy_server localhost:3000 连接到 nginx 配置文件(docker-compose 项目)
【发布时间】:2020-08-07 11:22:22
【问题描述】:

我在两个不同的文件夹中有以下 docker-compose.yml 文件:

~/front/docker-compose.yml~/api/docker-compose.yml

我需要将 proxy_server localhost:3000(来自前端)连接到 nginx 配置文件(来自 api)。我可能会错过什么?

这里是 ngix 配置文件:

server {
listen 80;
index index.html;
server_name localhost;
error_log  /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;

}


server {
listen          80;             # the port nginx is listening on
server_name     client.localhost;    # setup your domain here


 location / {


    proxy_redirect                      off;
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    proxy_read_timeout          1m;
    proxy_connect_timeout       1m;
    proxy_pass                          http://127.0.0.1:3000/; # set the address of the Node.js instance here
}
}

当我docker-compose logs -f nginx 时,这是错误:

2020/08/07 10:50:10 [error] 28#28: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.16.1, server: client.localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:3000/favicon.ico", host: "client.localhost", referrer: "http://client.localhost/"

error after running docker-compose logs -f nginx

这里是 front/docker-compose.yml

version: "3.5"
services:
  client:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: client
    ports:
      - "3000:3000"
    networks:
      - client_esl
networks:
  client_esl:
   external:
      name : nginx_esl

api/docker-compose.yml

version: "3.5"

networks:
  esl:

services:
  site:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - esl

前端文件夹中的 Dockfile

FROM 节点:12.4-alpine

运行mkdir -p /usr/src/nuxt-app

运行WORKDIR /usr/src/nuxt-app

运行apk update && apk upgrade

运行apk add git

复制。 /usr/src/nuxt-app/

运行npm install

运行npm run build

曝光 3000

ENV NUXT_HOST=0.0.0.0

ENV NUXT_PORT=3000

CMD [ "npm", "start" ]

【问题讨论】:

  • 你需要将它们组装到同一个docker网络中。

标签: node.js docker nginx docker-compose localhost


【解决方案1】:

如果要将 2 个容器连接在一起,有几种选择:

  • 像你一样将端口暴露给主机 (3000:3000),然后你可以从其他容器使用这个端口 + 主机 IP(不是 localhost,因为它指的是容器本身)
  • 不要暴露端口,如果容器在同一个网络中,请使用容器名称 + 端口,如 client:3000

【讨论】:

    【解决方案2】:

    在同一网络上运行容器并在您的 Nginx 配置上使用 proxy_pass : client:3000,因为您的节点应用程序的容器名称是客户端

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2019-08-06
      相关资源
      最近更新 更多