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

时间:2018-07-14 17:20:58

标签: docker nginx docker-compose dockerfile

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

/docker_compose.yml

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

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

FROM nginx:alpine

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

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

proxy / proxy.conf

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

SQLFiddle Example

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

1 个答案:

答案 0 :(得分:0)

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

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

如果您只希望将代理(nginx)暴露给外界并通过它进行所有连接,则您可以使用shouldn't need to expose端口8000,因为默认情况下它们是一起包含在隔离的网络中。

有帮助吗?