gpt4 book ai didi

node.js - 连接上游时出错。 Docker 撰写,NodeJS,Nginx

转载 作者:行者123 更新时间:2023-12-02 21:30:56 24 4
gpt4 key购买 nike

我正在尝试为 Nginx 和 NodeJS(使用 React)设置一个 docker compose。但是得到以下错误:*2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.1, server: ~.*, request: "GET /favicon.ico HTTP/1.1", upstream: "http://192.168.0.2:3000/favicon.ico", host: "localhost", referrer: "http://localhost/"曾尝试更改文件nginx.conf,添加depends_on,链接参数,向default.conf添加各种配置,从windows和linux启动,使用其他端口,均无济于事。
有了这个配置,结果收集了其他项目,除了这个,每个人都在工作,但在结构上它与其余的相同,除了根文件夹“app”中的 .env 文件。
Node 的 Dockerfile:

FROM node:13.12.0-alpine

# set working directory
WORKDIR /home/node/app

# install app dependencies

COPY package*.json ./

RUN npm install react-scripts -g
RUN npm install

# add app
COPY . ./

# start app
CMD ["npm", "start"]
docker -compose.yml:
version: '3'

services:
nodejs:
build:
context: ../app
dockerfile: ./../docker/dockerfile/node/Dockerfile
container_name: user-node
restart: unless-stopped
volumes:
- ../app:/home/node/app
expose:
- 3000
nginx:
image: nginx:latest
container_name: forensic-nginx
volumes:
- ../app:/home/node/app
- ./logs/nginx:/var/log/nginx
- ./config/nginx:/etc/nginx/conf.d
ports:
- 80:80
depends_on:
- nodejs
default.conf (nginx):
server {
listen 80;
server_name ~.*;

location / {
proxy_pass http://user-node:3000;
}
}
项目结构:
- project
- app
- package.json *and more*
- docker
- confing
- nginx
- default.conf
- dockerfile
- node
- Dockerfile
docker-compose.yml
在打开 localhost:80 时出现错误 502: bad geteway

最佳答案

您需要使用 docker 网络。更改您的 docker-compose.yml像这样:

version: '3'

services:
nodejs:
networks:
- custom-network
build:
context: ../app
dockerfile: ./../docker/dockerfile/node/Dockerfile
container_name: user-node
restart: unless-stopped
volumes:
- ../app:/home/node/app
expose:
- 3000
nginx:
networks:
- custom-network
image: nginx:latest
container_name: forensic-nginx
volumes:
- ../app:/home/node/app
- ./logs/nginx:/var/log/nginx
- ./config/nginx:/etc/nginx/conf.d
ports:
- 80:80
depends_on:
- nodejs

networks:
custom-network:
driver: bridge
你的 nginx 配置应该是这样的:
server {
listen 80;
server_name ~.*;

location / {
proxy_pass http://nodejs:3000;
}
}
如果您在 localhost 上使用端口 80 时遇到问题,请尝试更改 docker-compose.yml 的 nginx 部分中的映射使用另一个端口。例如
    ports:
- 8088:80

关于node.js - 连接上游时出错。 Docker 撰写,NodeJS,Nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64499421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com