带有nginx的502坏网关和docker里面的节点

时间:2017-06-08 16:35:10

标签: node.js nginx docker docker-compose dockerfile

我正在尝试在他们自己的docker容器和从nginx到node的代理中运行node和nginx。我首先尝试了没有docker 的配置,但它确实有效。但是,在使用泊坞窗时,它无效,并在尝试连接Status Code:502 Bad Gateway时提供http://localhost/

节点服务器

var http = require('http');
http.createServer(function (req, res) {
    res.setHeader('content-type', 'text/html');
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Request-Headers', '*');
    res.setHeader('Access-Control-Request-Method', '*');
    res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
    res.setHeader('Access-Control-Allow-Headers', '*');
    res.end('<html>Hey</html>');
}).listen(3001);

搬运工-compose.yml

version: '3'
services:
  app:
    build: ./servers
  nginx:
    build: ./nginx
    depends_on: 
      - app
    links:
      - app
    volumes:
      - ./nginx/conf:/etc/nginx/conf.d:ro
    ports: 
      - 80:80

nginx conf

server {
        listen 80;
        listen [::]:80;

        location / {
                proxy_pass http://app;
        }
}

app dockerfile

FROM node:alpine
RUN mkdir /app/
COPY ./server.js /app
EXPOSE 3001
WORKDIR /app
CMD node server

nginx dockerfile

FROM nginx:alpine
RUN rm /etc/nginx/conf.d/*

1 个答案:

答案 0 :(得分:1)

这现在看起来像一个愚蠢的问题,但我误以为docker将容器链接到它打开的任何端口而没有指定端口。事实证明,我所遵循的所有示例都使用了另一端的端口80。无论如何,足以为自己辩护,我所要做的就是让js服务器在80端口监听。

http.createServer(function (req, res) {
   ...
}).listen(80);

或者我相信我可以在nginx conf中使用端口3001

    location / {
            proxy_pass http://app:3001;
    }