502 Bad Gateway 在 Elastic Beanstalk 上部署 Express Generator 模板 [英] 502 Bad Gateway Deploying Express Generator Template on Elastic Beanstalk

查看:31
本文介绍了502 Bad Gateway 在 Elastic Beanstalk 上部署 Express Generator 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 express 生成器创建了一个简单的 express 应用,在 dev 上启动时可以在 localhost:3000 上正常工作.

I used the express generator to create a simple express app, which when started on dev works fine on localhost:3000.

当我使用 eb 命令 - git aws.push 将其推送到弹性 beantalk 时,我在生产服务器上收到 502 错误.

When I push this to elastic beanstalk using the eb command-- git aws.push, however, I get a 502 error on the production server.

查看日志,我得到的错误是:

Looking into the logs, the error I get is:

2014/04/01 19:29:40 [error] 24204#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.2.178, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "macenvexp-env-hqv9ucmzev.elasticbeanstalk.com"
2014/04/01 19:29:40 [error] 24204#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.2.178, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "macenvexp-env-hqv9ucmzev.elasticbeanstalk.com"

我使用的是默认的 nginx 配置.当我在没有 Express 的情况下运行 node.js 示例应用程序时,它工作正常.这是 app.js 中的 express 代码:

I'm using the default nginx configuration. When I run a node.js sample app without Express, it works fine. Here's the express code in app.js:

var express = require('express');
var http = require('http');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes');
var users = require('./routes/user');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);

app.get('/', routes.index);
app.get('/users', users.list);

/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.render('error', {
        message: err.message,
        error: {}
    });
}); 


module.exports = app;

这是 package.json 文件:

And here's the package.json file:

{
  "name": "macEnvExp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "DEBUG=macEnvExp node bin/www"
  },
  "dependencies": {
    "express": "~3.4.8",
    "static-favicon": "~1.0.0",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4",
    "jade": "~1.3.0"
  }
}

这里是 bin/www:

And here is bin/www:

#!/usr/bin/env node
var debug = require('debug')('my-application');
var app = require('../app');
app.configure(function(){
app.set('port', process.env.PORT || 3000);
});
console.log(app.get('port'));
var server = app.listen(app.get('port'), function() {
  debug('Express server listening on port ' + server.address().port);
});

推荐答案

为了清楚起见,我将从评论中给出答案.

For clarity, I'll state the answer from the comments.

AWS ELB 在 npm start 之前运行 node app.js.node app.js 不会报错,但不会打开任何端口.

AWS ELB runs node app.js BEFORE npm start. node app.js doesn't give an error, but it doesn't open any ports.

解决方案是简单地将 app.js 重命名为除 server.js(即 main.js)之外的任何其他内容,并在bin/www 通过在/bin/www 文件中指向它: var app = require('../app'); to var app = require('../main');

The solution is to simply rename app.js to anything else except server.js (ie main.js) and reference that in bin/www by pointing to it in the /bin/www file: var app = require('../app'); to var app = require('../main');

那么它应该可以正常工作了!

Then it should be working correctly!

为了清楚起见,这是我的目录的样子:

For clarity, here is what my directory looks like:

package.json 文件将在 ELB 启动应用程序服务器时被调用.这里有运行启动脚本的指令node bin/www

The package.json file will get called by ELB when it launches the application server. Here it has the instruction to run the start script node bin/www

这是运行的 bin/www 文件.我们看到 ../mainapp.set('port'...) 的要求

This is the bin/www file that gets run. We see the require to ../main and the app.set('port'...)

然后是运行路由和所有的 main.js 文件:

Then the main.js file that runs the routing and all:

当我创建项目时,main.js 文件被命名为 app.js.这导致的问题是基于优先级 ELB 启动序列.ELB 将启动应用程序并首先检查 app.js 是否存在 -- 如果存在,则运行 node app.js,否则将检查 >package.json 存在并尝试运行 npm start.当 main.js 的名称为 app.js 时,ELB 试图通过运行它来启动整个应用程序.但是这个文件没有打开任何端口.

When I created the project, the main.js file was named app.js. The problem this caused was based on the priority ELB start sequences. ELB will launch the application and check first to see if app.js exists -- if it does exist, it runs node app.js, otherwise it will check if package.json exists and try to run npm start. When the main.js had the name app.js ELB tried to start the whole application by running it. However this file doesn't open any ports.

这篇关于502 Bad Gateway 在 Elastic Beanstalk 上部署 Express Generator 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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