使用Nginx和Docker加载Balacing Node.js应用程序

问题描述:

我正在关注有关如何在Docker上配置Nginx Server的YouTube教程

I am following this YouTube tutorial on how to configure Nginx Server on Docker

/docker_compose.yml

/docker_compose.yml

version: '3'
services:
  nodecluster:
        build: nodecluster
        ports:
        - "49160:8000"
  proxy:
    build: proxy
    ports:
    - "80:80"  

nodecluster/Dockerfile

nodecluster/Dockerfile

FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 8000
CMD [ "npm", "start" ]

proxy/Dockerfile

proxy/Dockerfile

FROM nginx:alpine

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

COPY proxy.conf /etc/nginx/conf .d/

proxy/proxy.conf

proxy/proxy.conf

listen 80;
server {
    location / {
        proxy_pass http://nodecluster;
    }
}

Docker命令提示符详细信息

但是当我点击localhost而不是教程时,我收到nginx 502错误的网关错误.我尝试了localhost:49160,它正在工作并提供正常的输入. 那么如何正确地将传入请求映射到nodecluster

But When I hit localhost instead of in the tutorial I am getting nginx 502 bad gateway error . I tried localhost:49160 it is working and giving normal input. So how to correctly map the incoming request to the nodecluster

您似乎需要将nginx配置设置为使用正确的端口:

It looks like you need to set your nginx config to use the proper port:

listen 80;
server {
    location / {
        proxy_pass http://nodecluster:8000;
    }
}

如果您只想公开端口8000,则不需要公开端口代理(nginx)到外界,并通过它具有所有连接,因为默认情况下,它们一起包含在隔离的网络中.

And you shouldn't need to expose port 8000 if you only wish to expose the proxy (nginx) to the outside world and have all the connections through it since by default they're included together in an isolated network.

有帮助吗?