NGINX和Docker-Compose:在上游找不到主机 [英] NGINX and Docker-Compose: host not found in upstream
问题描述
我正在尝试让docker-compose运行NGINX反向代理,但是遇到了问题。我知道我正在尝试的内容似乎是可能的,如下所示:
I'm trying to get docker-compose to run an NGINX reverse-proxy and I'm running into an issue. I know that what I am attempting appears possible as it is outlined here:
https://dev.to/domysee/setting-up-a-reverse-proxy-with-nginx-and- docker-compose-29jg
在这里:
我的应用程序非常简单-它具有前端和后端(nextjs和nodejs),我将它们与nginx实例一起放入了docker-compose。
My application is very simple - it has a front end and a back end (nextjs and nodejs), which I've put in docker-compose along with an nginx instance.
这是docker-compose fi le:
Here is the docker-compose file:
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
运行它。
Both of these are very similar to the digitalOcean example and I can't think of how they would be different enough to cause errors. I run it with a simple docker-compose up -d --build
.
当我进入 localhost:80
我得到页面找不到
,这是我的docker日志的结果-
When I go to localhost:80
I get page could not be found
, and here is the result of my docker logs -
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
我对可能出了什么问题很迷茫。如果有人有任何想法,请告诉我。谢谢。
I'm pretty lost as to what could be going wrong. If anyone has any ideas please let me know. Thank you.
编辑:请参阅下面的解决方案
SEE SOLUTION BELOW
推荐答案
code> nginx Web服务器位于网络 app-network
网络上,该网络与其他两个没有服务的服务不同网络定义。如果未定义网络,则docker-compose将创建一个默认网络供他们共享。
The nginx
webserver is on the network app-network
which is a different network than the other two services which don't have a network defined. When no network is defined docker-compose will create a default network for them to share.
将网络设置复制到其他两个服务中,或者从网络服务器服务中删除网络设置。
Either copy the network setting to both of the other services or remove the network setting from the webserver service.
这篇关于NGINX和Docker-Compose:在上游找不到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!