使用Nginx和Docker加载Balacing Node.js应用程序 [英] Load Balacing Nodejs application using Nginx and Docker

查看:116
本文介绍了使用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.

有帮助吗?

这篇关于使用Nginx和Docker加载Balacing Node.js应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆