簡體   English   中英

Nginx 通過 Docker-compose 運行時找不到上游節點應用程序

[英]Nginx can't find upstream node app when running via Docker-compose

我有一個超級簡單的 Node 應用程序和一個 Nginx 配置,它充當 Node 應用程序的反向代理。 如果我在本地運行 Nginx(通過自制軟件)和 Node 應用程序,一切正常。 如果我在端口 8080 訪問由 Nginx 配置定義的服務器,我會從運行在端口 3000 上的節點應用程序獲得 output。

我一直在嘗試將這個簡單的設置轉換為使用 Docker 並編寫了以下 Docker-compose 文件:

version: '3.0'
services:
  web:
    build: .
    ports:
      - 3000:3000
  nginx:
    build:
      context: .
      dockerfile: Dockerfile.nginx
    ports:
      - 8080:8080

在運行docker-compose up ,圖像已構建並且控制台中沒有錯誤消息。 在訪問localhost:3000時,我得到了 Node 應用程序的響應,但在訪問localhost:8080時,我得到一個 Nginx 502 錯誤頁面和終端中的以下錯誤:

connect() 連接到上游時失敗(111:連接被拒絕),客戶端:172.18.0.1,服務器:localhost,請求:“GET / HTTP/1.1”,上游:“ http://127.0.0.1:3000/ ”,主機:“本地主機:8080”

我的節點應用程序的 Dockerfile 如下所示:

FROM node:carbon

WORKDIR /app

ADD . /app

RUN npm install

CMD ["node", "."]

EXPOSE 3000

Dockerfile.nginx 看起來像這樣:

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

nginx.conf 看起來像這樣:

events {
  worker_connections  1024;
}

http {

  upstream node_app {
    server 127.0.0.1:3000;
  }

  server_tokens off;

  # Define the MIME types for files.
  include       mime.types;
  default_type  application/octet-stream;

  # Speed up file transfers by using sendfile()
  # TODO: Read up on this
  sendfile on;

  server {
    listen 8080;
    server_name localhost;

    location / {
      proxy_pass http://node_app;
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
    }
  }
}

On starting Docker up I can see that Nginx is running on port 8080 (because I see the 502 Nginx page) and I can see that the node app is running (because I can visit it at localhost:3000). 我不知道為什么我從 nginx 得到 502。

我嘗試過使用各種不同的東西,比如使用links來鏈接容器和depends_on ,但似乎沒有任何區別。 我還使用docker-compose up --build來確保每次進行更改時都不會緩存以前的構建。

編輯:似乎使它起作用的是在 docker-compose 中添加一個 container_name 屬性:

  web:
    container_name: nodeapp
    build:
      context: .
      dockerfile: Dockerfile.node
    ports:
      - 3000:3000

然后在 nginx.conf 的上游 node_app 配置中使用該容器名稱:

  upstream node_app {
    server nodeapp:3000;
  }

這對我來說沒有意義?!

問題在於,在您的Nginx配置中,您將Web服務的IP引用為127.0.0.1,這是運行docker容器的主機的環回地址。 這可能會起作用,具體取決於您的設置(操作系統,防火牆),也可能不起作用。

正確的方法是使nginx服務依賴於docker-compose.yml文件中的web服務,並更新Nginx配置以按名稱( web )而不是IP地址引用該Web服務。 在這里您可以找到與docker compose相關的更多信息,具體取決於功能。

更新后的docker-compose.yml文件為:

version: '3.0'
services:
  web:
    build: .
  nginx:
    build:
      context: .
      dockerfile: Dockerfile.nginx
    ports:
      - 8080:8080
    depends_on:
      - web

請注意,我已經停止公開web服務的端口。 可能是您需要保留它以監視Web服務,但nginx服務不是必需的。

通過對docker-compose.yml文件的此更新,Nginx配置將如下所示:

events {
  worker_connections  1024;
}

http {

  upstream node_app {
    server web:3000;
  }

  server_tokens off;

  # Define the MIME types for files.
  include       mime.types;
  default_type  application/octet-stream;

  # Speed up file transfers by using sendfile()
  # TODO: Read up on this
  sendfile on;

  server {
    listen 8080;
    server_name localhost;

    location / {
      proxy_pass http://node_app;
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM