NGINX和Docker-Compose:在上游找不到主机

时间:2019-04-10 15:20:46

标签: docker nginx networking docker-compose reverse-proxy

我正在尝试让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,这是我的码头工人日志的结果-

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

对于可能出了什么问题,我感到很困惑。如果有人有任何想法,请告诉我。谢谢。

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

1 个答案:

答案 0 :(得分:1)

nginx Web服务器位于网络app-network上,该网络与未定义网络的其他两个服务是不同的网络。如果未定义网络,则docker-compose将创建一个默认网络供他们共享。

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