502 Bad Gateway在弹性Beanstalk上部署Express Generator模板 [英] 502 Bad Gateway Deploying Express Generator Template on Elastic Beanstalk
问题描述
我使用快速生成器来创建一个简单的快速应用程序,当启动时,dev在本地主机上工作正常: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,但是,我在生产服务器上收到一个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.
查看日志,我得到的错误是: p>
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中的快速代码:
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运行 node app.js
BEFORE npm start
。 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');
那么应该是工作正常!
为了清楚起见,这里是我的目录:
For clarity, here is what my directory looks like:
当启动应用程序服务器时,ELB将调用 package.json
文件。这里有指令运行起始脚本 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
文件。我们看到需要 ../ main
和 app.set('port'...)
This is the bin/www
file that gets run. We see the require to ../main
and the app.set('port'...)
然后, code> main.js 运行路由和所有文件:
Then the main.js
file that runs the routing and all:
当我创建项目时, main.js
文件被命名为code> 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在弹性Beanstalk上部署Express Generator模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!