[英]Docker-Compose Nginx (with static React) and Nginx
I am currently stuck on making nginx
proxy to the node
load balancer.我目前坚持将
nginx
代理到node
负载均衡器。 It gives the following error when making a request on 185.146.87.32:5000/
:在
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"
I managed to make this work on a local system, but now I am trying to make it work on a remote server.我设法让它在本地系统上工作,但现在我正试图让它在远程服务器上工作。
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"
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"
This is the docker-compose
:这是
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
The Dockerfile
for node is:节点的
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
The config file for nginx is: 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;
}
}
The Dockerfile
for nginx is: 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;"]
don't expose your backend to world,不要将你的后端暴露给世界,
create a docker network for your services, then expose nginx, it's the best practice, but in your case you didnt specify backend ports in nginx.conf为您的服务创建一个 docker 网络,然后公开 nginx,这是最佳实践,但在您的情况下,您没有在 nginx.conf 中指定后端端口
upstream domain {
least_conn;
server backend_1:5000 weight=1;
server backend_2:5000 weight=1;
}
you should do below:你应该在下面做:
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
but after all, i recommend jwilder/nginx-proxy但毕竟,我推荐jwilder/nginx-proxy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.