简体   繁体   English

无法将 proxy_server localhost:3000 连接到 nginx 配置文件(docker-compose 项目)

[英]Cannot connect proxy_server localhost:3000 to nginx config file (docker-compose project)

I have the following docker-compose.yml files located in two different folders:我有以下 docker-compose.yml 文件位于两个不同的文件夹中:

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

I need to connect proxy_server localhost:3000 (from frontend) to nginx config file (from api).我需要将 proxy_server localhost:3000 (来自前端)连接到 nginx 配置文件(来自 api)。 What could I be missing?我会错过什么?

Here is the ngix config file:这是 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
}
}

When I do docker-compose logs -f nginx , this is the error:当我做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运行 docker-compose logs -f nginx 后出错

Here is the front/docker-compose.yml :这是前面/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 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 on front folder前端文件夹上的 Dockfile

FROM node:12.4-alpine FROM 节点:12.4-alpine

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

RUN WORKDIR /usr/src/nuxt-app运行WORKDIR /usr/src/nuxt-app

RUN apk update && apk upgrade运行apk update && apk upgrade

RUN apk add git运行apk add git

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

RUN npm install运行npm install

RUN npm run build运行npm run build

EXPOSE 3000曝光 3000

ENV NUXT_HOST=0.0.0.0 ENV NUXT_HOST=0.0.0.0

ENV NUXT_PORT=3000 ENV NUXT_PORT=3000

CMD [ "npm", "start" ] CMD [“npm”,“开始”]

If you want to connect 2 containers together, there are several options:如果要将 2 个容器连接在一起,有几种选择:

  • Expose the port to the host as you did (3000:3000), then you can use this port + host IP (not localhost as it refer to the container itself) from the other containers像您一样将端口公开给主机(3000:3000),然后您可以从其他容器使用此端口 + 主机 IP(不是 localhost,因为它指的是容器本身)
  • Don't expose the port, if the containers are in same network use container name + port like client:3000不要暴露端口,如果容器在同一个网络中,请使用容器名称 + 端口,如 client:3000

run the containers on the same network and on your Nginx config use proxy_pass: client:3000 since your container name for your node app is client在同一网络和 Nginx 配置上运行容器使用proxy_pass: client:3000因为您的节点应用程序的容器名称是 client

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 Docker-compose 连接到 MongoDB - cannot connect to MongoDB with Docker-compose 多个 React 应用程序的 Nginx 配置(docker-compose) - Nginx config for multiple react apps (docker-compose) docker-compose 不为 localhost:3000 下的 Nuxt.js 网站提供服务 - docker-compose not serving Nuxt.js website under localhost:3000 无法使用 Docker-compose 与 Nodejs 连接到 mongoDB - Cannot connect to mongoDB with Nodejs using Docker-compose nodejs服务器无法在同一个Docker-Compose yml文件中链接Redis和MongoDB - nodejs server cannot link Redis and MongoDB in the same Docker-Compose yml file 无法使用 docker-compose 通过 Sequelize 连接到 Postgres 数据库 - Cannot connect to Postgres database with Sequelize using docker-compose docker-compose:无法将本地模块添加到项目中,找不到 package.json 文件 - docker-compose: cannot add local module to project, canno find package.json file Docker-Compose Nginx(与 static 反应)和 Z62E0B5B350C9E3D2BA919AA801DZ94 - Docker-Compose Nginx (with static React) and Nginx keystonejs docker-compose 在文件更改时重新启动服务器 - keystonejs docker-compose restarting a server when file changes 移动 docker-compose.yml 文件时,Nginx 图像的 docker-compose 不起作用 - docker-compose of Nginx image doesn't work when the docker-compose.yml file is moved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM