Nginx 111错误

时间:2017-03-08 14:04:58

标签: node.js nginx docker-compose

我尝试将Nginx用于节点应用程序的反向代理。我的开发环境是在docker中设置的。 这是我的docker-compose.yml文件

version: '2'

services: 
  web:
    build: ./nginx
    links:
      - engine
    ports: 
      - "8080:80"
      - "8081:443"
  engine:
    build: ./engine
    volumes: 
      - ./engine:/var/www/engine
    ports: 
      - "5000:5000"

引擎(节点)服务的dockerfile

FROM ubuntu:trusty

# Install Node.js and other dependencies
RUN apt-get update && \
    apt-get -y install curl && \
    apt-get -y install git && \
    apt-get -y install wget && \
    curl -sl https://deb.nodesource.com/setup_6.x | sudo -E bash - && \
    apt-get -y install python build-essential nodejs

# Install yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && \
    apt-get -y install yarn && \
    export PATH="$PATH:`yarn global bin`"

# Install pm2, gulp, typescript
RUN yarn global add pm2 && \
    yarn global add gulp && \
    yarn global add typescript && \
    yarn global add typings

# Define working directory
WORKDIR /var/www/engine

# Install dependecies
RUN yarn install

# Expose export
EXPOSE 5000

# Run app
CMD yarn start

nginx dockerfile

FROM nginx

ADD ./nginx.conf /etc/nginx/nginx.conf

WORKDIR /var/www

nginx.conf

worker_processes 4;

events { worker_connections 1024; }

http {
    upstream app {
        server engine:5000;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://app;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
}

我有一个在引擎服务中运行的基本节点服务

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(5000, 'localhost');
console.log('Server running at http://localhost:5000/');

我可以在容器输出中看到日志消息

尝试通过http://localhost:8080访问引擎服务会导致502错误网关,并且日志会显示

[error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.25.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.25.0.2:5000/favicon.ico", host: "localhost:8080", referrer: "http://localhost:8080/

我无法弄清楚哪个配置错误并导致问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

修正了问题,结果我的节点应用已明确绑定到'localhost'。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(5000);
console.log('Server running at http://localhost:5000/');