gpt4 book ai didi

docker - NGINX 和 Docker-Compose : host not found in upstream

转载 作者:行者123 更新时间:2023-12-02 18:09:27 30 4
gpt4 key购买 nike

我正在尝试让 docker-compose 运行 NGINX 反向代理,但我遇到了一个问题。我知道我正在尝试的事情似乎是可能的,因为它概述如下:

https://dev.to/domysee/setting-up-a-reverse-proxy-with-nginx-and-docker-compose-29jg

和这里:

https://www.digitalocean.com/community/tutorials/how-to-secure-a-containerized-node-js-application-with-nginx-let-s-encrypt-and-docker-compose#step-2-%E2%80%94-defining-the-web-server-configuration

我的应用程序非常简单 - 它有一个前端和一个后端(nextjs 和 nodejs),我将它们与一个 nginx 实例一起放入 docker-compose 中。

这是 docker-compose 文件:

version: '3'

services:
nodejs:
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:8000"
container_name: nodejs
restart: unless-stopped
nextjs:
build:
context: ../.
dockerfile: Dockerfile
ports:
- "3000:3000"
container_name: nextjs
restart: unless-stopped
webserver:
image: nginx:mainline-alpine
container_name: webserver
restart: unless-stopped
ports:
- "80:80"
volumes:
- web-root:/var/www/html
- ./nginx-conf:/etc/nginx/conf.d
depends_on:
- nodejs
- nextjs
networks:
- app-network


volumes:
certbot-etc:
certbot-var:
web-root:
driver: local
driver_opts:
type: none
device: /
o: bind

networks:
app-network:
driver: bridge

这是 nginx 文件:
server {
listen 80;
listen [::]:80;

root /var/www/html;
index index.html index.htm index.nginx-debian.html;

server_name patientplatypus.com www.patientplatypus.com localhost;

location /back {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

proxy_pass http://nodejs:8000;
}

location / {
proxy_pass http://nextjs:3000;
}

location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
}

这两个都与 digitalOcean 示例非常相似,我想不出它们之间的差异会如何导致错误。我用一个简单的 docker-compose up -d --build 运行它.

当我去 localhost:80我收到 page could not be found ,这是我的 docker 日志的结果 -
patientplatypus:~/Documents/patientplatypus.com/forum/back:10:03:32$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c2e4e25e6d9 nginx:mainline-alpine "nginx -g 'daemon of…" 2 minutes ago Restarting (1) 14 seconds ago webserver
213e73495381 back_nodejs "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8000->8000/tcp nodejs
03b6ae8f0ad4 back_nextjs "npm start" 2 minutes ago Up 2 minutes 0.0.0.0:3000->3000/tcp nextjs
patientplatypus:~/Documents/patientplatypus.com/forum/back:10:05:41$docker logs 9c2e4e25e6d9
2019/04/10 15:03:32 [emerg] 1#1: host not found in upstream "nodejs" in /etc/nginx/conf.d/nginx.conf:20

我对可能出什么问题感到很迷茫。如果有人有任何想法,请告诉我。谢谢你。

编辑:请参阅下面的解决方案

最佳答案

nginx网络服务器在网络上 app-network这是一个与其他两个没有定义网络的服务不同的网络。当没有定义网络时,docker-compose 将创建一个默认网络供他们共享。

将网络设置复制到其他两个服务或从网络服务器服务中删除网络设置。

关于docker - NGINX 和 Docker-Compose : host not found in upstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55616166/

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