502网关部署前preSS生成模板弹性魔豆 [英] 502 Bad Gateway Deploying Express Generator Template on Elastic Beanstalk
问题描述
我用前preSS生成器来创建一个简单的EX preSS应用程序,这在开发启动时正常工作在localhost:3000
当我把这个使用EB command--混帐aws.push弹性魔豆,但是,我得到一个502错误在生产服务器上。
展望日志中,我得到的错误是:
2014年4月1日十九时29分40秒[错误] 24204#0:* 1连接()失败(111:连接被拒绝),而连接到上游,客户端:172.31。 2.178,服务器:,请求:GET / HTTP / 1.1,上游http://127.0.0.1:8081/,主持人:macenvexp-env-hqv9ucmzev.elasticbeanstalk.com
2014年4月1日19时29分40秒[错误] 24204#0:* 1连接()失败(111:连接被拒绝),而连接到上游,客户端:172.31.2.178,服务器:,请求:GET /图标。 ICO HTTP / 1.1,上游http://127.0.0.1:8081/favicon.ico,主持人:macenvexp-env-hqv9ucmzev.elasticbeanstalk.com
我使用的是默认的nginx的配置。当我运行一个Node.js的示例应用程序没有防爆preSS,它工作正常。这里的前preSS code。在app.js:
VAR EX preSS =需要('EX preSS');
VAR HTTP =需要('HTTP');
VAR路径=要求('路径');
VAR图标=需要('静态图标');
VAR记录=需要('摩根');
VAR cookieParser =需要('cookie的解析器');
VAR bodyParser =需要('身体解析器');
VAR路线=需要('./路线');
VAR用户=需要('./路线/用户);
VAR应用= EX preSS();
//视图引擎设置
app.set('意见',path.join(__目录名称,若干意见));
app.set(视图引擎','玉');
app.use(图标());
app.use(记录器('开发'));
app.use(bodyParser.json());
app.use(bodyParser.urlen codeD());
app.use(cookieParser());
app.use(如press.static(path.join(__目录名称,公开)));
app.use(app.router);
app.get('/',routes.index);
app.get('/用户,users.list);
///抓404和转发到错误处理程序
app.use(功能(REQ,水库,下一个){
VAR ERR =新的错误(未找到);
err.status = 404;
下一个(ERR);
});
///错误处理
//开发错误处理程序
//将打印堆栈跟踪
如果(app.get('ENV')===发展){
app.use(功能(ERR,REQ,水库旁){
res.render('错误',{
消息:err.message,
错误:ERR
});
});
}
//生产错误处理程序
//无踪迹泄露给用户
app.use(功能(ERR,REQ,水库旁){
res.render('错误',{
消息:err.message,
错误: {}
});
});
module.exports =应用;
和这里的的package.json文件:
{
名:macEnvExp
版本:0.0.1,
私:真实,
脚本:{
开始:DEBUG = macEnvExp节点斌/ WWW
},
依赖:{
EX preSS:〜3.4.8
静态图标:〜1.0.0
摩根:〜1.0.0
饼干分析器:〜1.0.1
身体分析器:〜1.0.0
调试:〜0.7.4
玉:〜1.3.0
}
}
这里是斌/ WWW:
#!的/ usr / bin中/ env的节点
变种调试=要求(调试)(我的应用);
VAR应用=需要('../应用程序);
app.configure(函数(){
app.set('口',process.env.PORT || 3000);
});
的console.log(app.get('端口'));
VAR服务器= app.listen(app.get('口'),函数(){
调试('前preSS服务器侦听端口+ server.address()端口。);
});
为了清楚起见,我将说明从意见的答案。
AWS ELB运行节点app.js
在 NPM启动
。 节点app.js
不给一个错误,但它不会打开任何端口。
解决方案是简单地重命名 app.js
来任何东西,除了 server.js
(即 main.js
)和参考,在斌/ WWW通过指向其在/斌/ WWW文件: VAR应用=需要('../应用程序 );
到 VAR应用=需要('../主');
那么就应该工作正常!
为了清楚起见,这里是我的目录如下:
在的package.json
文件将获得由ELB时调用启动应用程序服务器。在这里,它的指令运行启动脚本节点斌/ WWW
这是斌/ WWW
文件,该文件被运行。我们看到的要求,以 ../主
和 app.set('口'...)
然后运行路由和所有的 main.js
文件:
当我创建的项目,在 main.js
文件被命名为 app.js
。这导致的问题是基于优先级的ELB启动序列。 ELB将启动应用程序,并检查首先看看 app.js
的存在 - 如果它确实存在,它运行节点app.js
,否则会检查的package.json
的存在,并尝试运行 NPM启动
。
当 main.js
有名称 app.js
ELB试图通过运行它来启动整个应用程序。但此文件不打开任何端口。
I used the express generator to create a simple express app, which when started on dev works fine on localhost:3000.
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"
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;
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"
}
}
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 runs node app.js
BEFORE npm start
. node app.js
doesn't give an error, but it doesn't open any ports.
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:
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
This is the bin/www
file that gets run. We see the require to ../main
and the app.set('port'...)
Then the main.js
file that runs the routing and all:
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网关部署前preSS生成模板弹性魔豆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!