node.js – “502 Bad Gateway”将hapi.js部署到AWS Beanstalk?

我用以下代码构建了一个非常简单的hapi.js应用程序.

var Hapi = require('hapi');
var server = new Hapi.Server(3000);

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
        reply('Hello, world!');
    }
});

server.start(function () {
    console.log('Server running at:', server.info.uri);
});

但是,部署时我不断收到“502 Bad Gateway”错误.我正在使用标准拉链和放大器上传方法进行部署. zip文件包含一个带有上面代码的service.js文件和一个package.json文件,如下所示.

{
  "name": "hapi_aws_testing",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "hapi": "^6.4.0"
  },
  "engines"        : {
    "node": "0.10.26"
  }
}

我已尝试部署删除了node.js引擎部分,并将其设置为0.10.29,然后意识到在1.0.4 AMI映像上Beanstalk中可用的node.js版本为0.10.26,因此更改了它这个版本.我在当地尝试过,一切运行良好.

在错误日志中,我有以下两个日志显示代码正在运行…

-------------------------------------
/var/log/nodejs/nodejs.log
-------------------------------------
Server running at: http://ip-172-31-3-9:3000

然后当我尝试使用浏览器访问服务器时出现错误.

-------------------------------------
/var/log/nginx/error.log
-------------------------------------
2014/08/12 02:07:24 [error] 3457#0: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.13.177, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "consociation-test.elasticbeanstalk.com"
2014/08/12 02:07:26 [error] 3457#0: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.13.177, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "consociation-test.elasticbeanstalk.com"
最佳答案
看起来您的服务器正在侦听端口3000,但是从nginx日志文件中,它设置为代理侦听端口8081的节点应用程序(请参阅“上游:”部分).

你可能想尝试使用PORT环境变量,而不是将其硬编码为任何值 – 我很确定EB将此暴露给你的应用程序.这将确保运行时更新不会破坏您的设置.

转载注明原文:node.js – “502 Bad Gateway”将hapi.js部署到AWS Beanstalk? - 代码日志