簡體   English   中英

Docker-Compose Nginx(與 static 反應)和 Z62E0B5B350C9E3D2BA919AA801DZ94

[英]Docker-Compose Nginx (with static React) and Nginx

我目前堅持將nginx代理到node負載均衡器。 185.146.87.32:5000/上發出請求時出現以下錯誤:

2020/06/01 13:23:09 [warn] 6#6: *1 upstream server temporarily disabled while connecting to upstream, client: 86.125.198.83, server: domain.ro, request: "GET / HTTP/1.1", upstream: "http://185.146.87.32:5002/", host: "185.146.87.32:5000"

我設法讓它在本地系統上工作,但現在我正試圖讓它在遠程服務器上工作。 BACKEND_SERVER_PORT_1=5001 BACKEND_SERVER_PORT_2=5002 BACKEND_NODE_PORT=5000 BACKEND_NGINX_PORT=80 CLIENT_SERVER_PORT=3000 ADMIN_SERVER_PORT=3006 NGINX_SERVER_PORT=80 API_HOST="http://domain.ro"

這是docker-compose

version: '3'
services:

#####################################
#   Setup for NGINX container
#####################################
nginx:
  container_name: domain_back_nginx
  build:
    context: ./nginx
    dockerfile: Dockerfile
  image: domain/domain_back_nginx
  ports:
    - ${BACKEND_NODE_PORT}:${BACKEND_NGINX_PORT}
  volumes:
    - ./:/usr/src/domain
  restart: always
#####################################
#   Setup for backend container
#####################################
backend_1:
  container_name: domain_back_server_1
  build:
    context: ./
    dockerfile: Dockerfile
  image: domain/domain_back_server_1
  ports:
    - ${BACKEND_SERVER_PORT_1}:${BACKEND_NODE_PORT}
  volumes:
    - ./:/usr/src/domain
  restart: always
  command: npm start
#####################################
#   Setup for backend container
#####################################
backend_2:
  container_name: domain_back_server_2
  build:
    context: ./
    dockerfile: Dockerfile
  image: domain/domain_back_server_2
  ports:
    - ${BACKEND_SERVER_PORT_2}:${BACKEND_NODE_PORT}
  volumes:
    - ./:/usr/src/domain
  restart: always
  command: npm start

節點的Dockerfile是:

FROM node:12.17.0-alpine3.9

RUN mkdir -p /usr/src/domain

ENV NODE_ENV=production

WORKDIR /usr/src/domain

COPY package*.json ./

RUN npm install --silent

COPY . .

EXPOSE 5000

nginx 的配置文件是:

upstream domain {
  least_conn;
  server backend_1 weight=1;
  server backend_2 weight=1;
}

server {
   listen 80;
   listen [::]:80;

   root /var/www/domain_app;
   server_name domain.ro www.domain.ro;
   location / {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://domain;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

Dockerfile的 Dockerfile 是:

FROM nginx:1.17-alpine as build

#!/bin/sh

RUN rm /etc/nginx/conf.d/default.conf

COPY default.conf /etc/nginx/conf.d

CMD ["nginx", "-g", "daemon off;"]

不要將你的后端暴露給世界,

為您的服務創建一個 docker 網絡,然后公開 nginx,這是最佳實踐,但在您的情況下,您沒有在 nginx.conf 中指定后端端口

upstream domain {
  least_conn;
  server backend_1:5000 weight=1;
  server backend_2:5000 weight=1;
}

你應該在下面做:

version: '3'
services:

#####################################
#   Setup for NGINX container
#####################################
nginx:
  container_name: domain_back_nginx
  build:
    context: ./nginx
    dockerfile: Dockerfile
  image: domain/domain_back_nginx
  networks:
    - proxy
  ports:
    - 5000:80
  volumes:
    - ./:/usr/src/domain
  restart: always
#####################################
#   Setup for backend container
#####################################
backend_1:
  container_name: domain_back_server_1
  build:
    context: ./
    dockerfile: Dockerfile
  image: domain/domain_back_server_1
  networks:
    - proxy

  ## always expose, just in case you missed it in Dockerfile, this will expose the port(s)
  ## just in defined networks
  expose:
    - 5000
  volumes:
    - ./:/usr/src/domain
  restart: always
  command: npm start
#####################################
#   Setup for backend container
#####################################
backend_2:
  container_name: domain_back_server_2
  build:
    context: ./
    dockerfile: Dockerfile
  image: domain/domain_back_server_2
  networks:
    - proxy

  ## always expose, just in case you missed it in Dockerfile, this will expose the port(s)
  ## just in defined networks
  expose:
    - 5000
  volumes:
    - ./:/usr/src/domain
  restart: always
  command: npm start

networks:
  proxy:
    external:
      name: proxy

但畢竟,我推薦jwilder/nginx-proxy

暫無
暫無

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

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