Nodejs 502 Bad Gateway 在 Elastic Beanstalk AWS 上部署 Express
Posted
技术标签:
【中文标题】Nodejs 502 Bad Gateway 在 Elastic Beanstalk AWS 上部署 Express【英文标题】:Nodejs 502 Bad Gateway Deploying Express on Elastic Beanstalk AWS 【发布时间】:2016-12-20 06:25:09 【问题描述】:我将 node js 应用程序部署到 AWS EBS。当我运行应用程序时,我收到错误“502 Bad Gateway”nginx/1.6.2。这是我在日志中发现的。
2016/08/13 08:46:03 [warn] 14418#0: duplicate MIME type "text/html" in /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf:42
2016/08/13 09:22:25 [error] 14421#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 118.36.218.138, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "cider-1.siwrszgawk.ap-northeast-1.elasticbeanstalk.com"
2016/08/13 09:22:25 [error] 14421#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 118.36.218.138, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "cider-1.siwrszgawk.ap-northeast-1.elasticbeanstalk.com", referrer: "http://cider-1.siwrszgawk.ap-northeast-1.elasticbeanstalk.com/"
我为此度过了一天。我找到了一些好的解决方案。 enter link description here(同样的问题) 但它不适用于我的代码。(我尝试了最选择的重命名解决方案) 虽然我已经阅读了 *** 上的其他资源,建议我将我的主文件从 app.js 重命名为 main.js 并将端口设置在 bin/www 文件夹中,但我觉得这不是我的应用程序的解决方案。
"name": "cidermics",
"version": "0.0.1",
"private": true,
"scripts":
"start": "node main.js"
,
"dependencies":
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"ejs": "~2.3.3",
"express": "~4.13.1",
"file-system": "^2.2.1",
"formidable": "^1.0.17",
"morgan": "~1.6.1",
"multer": "^1.1.0",
"node-dir": "^0.1.11",
"serve-favicon": "~2.3.0",
"xhr": "^2.2.0",
"passport" : "*",
"passport-local" : "*",
"connect-flash" : "*",
"express-session" : "*",
"req-flash" : "*"
是 main.js 我把名字从 app.js 改成了 main.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var debug = require('debug')('cidermics:server');
var http = require('http');
var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
var mysql = require("./routes/model/mysql");
var flash = require('req-flash');
var session = require('express-session');
var routes = require('./routes/index');
var users = require('./routes/users');
var admin = require('./routes/admin');
//route add
var about = require('./routes/about');
var cmn = require('./routes/cmn');
var consulting = require('./routes/consulting');
var contents = require('./routes/contents');
var member = require('./routes/member');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded( extended: false ));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session( secret: 'fortt', resave: true, saveUninitialized: true ))
app.use(passport.initialize());
app.use(passport.session());
//express.static ADD
app.use('/cid_about', express.static(__dirname + '/views/cid_about'));
app.use('/cid_cmn', express.static(__dirname + '/views/cid_cmn'));
app.use('/cid_consulting', express.static(__dirname + '/views/cid_consulting'));
app.use('/cid_contents', express.static(__dirname + '/views/cid_contents'));
app.use('/cid_member', express.static(__dirname + '/views/cid_member'));
app.use(flash());
app.use('/', routes);
app.use('/users', users);
app.use('/adm', admin);
//app.get
app.use('/',about);
app.use('/',cmn);
app.use('/',consulting);
app.use('/',contents);
app.use('/',member);
passport.use('local', new LocalStrategy(
usernameField : 'email',
passwordField : 'pw',
passReqToCallback : true
,function(req, email, pw, done)
mysql.select('select * from cider.cid_user where user_email ="'+email+'" and user_password = "'+pw+'"', function (err, data)
console.log("data");
console.log(data.length);
if(data.length < 1)
console.log('fail');
return done(null, false);
else
console.log('success');
return done(null, data);
if(err)
res.redirect('back');
);
));
passport.serializeUser(function(user, done)
done(null, user);
// if you use Model.id as your idAttribute maybe you'd want
// done(null, user.id);
);
passport.deserializeUser(function(user, done)
done(null, user);
);
// catch 404 and forward 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.status(err.status || 500);
res.render('error',
message: err.message,
error: err
);
);
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next)
res.status(err.status || 500);
res.render('error',
message: err.message,
error:
);
);
app.set('port', process.env.PORT || 80);
var server = app.listen(app.get('port'), function()
//http.createServer(app).listen(app.get('port'), function()
// console.log('Express server listening on port ' + app.get('port'));
debug('Express server listening on port ' + server.address().port);
);
/var/log/nodejs/nodejs.log
-------------------------------------
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm" "start"
npm ERR! node v4.4.6
npm ERR! npm v2.15.5
npm ERR! code ELIFECYCLE
npm ERR! cidermics@0.0.1 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cidermics@0.0.1 start script 'node app.js'.
npm ERR! This is most likely a problem with the cidermics package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node app.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs cidermics
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls cidermics
npm ERR! There is likely additional logging output above.
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm" "start"
npm ERR! node v4.4.6
npm ERR! npm v2.15.5
npm ERR! path npm-debug.log.3664877195
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall open
npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.3664877195'
npm ERR! at Error (native)
npm ERR! [Error: EACCES: permission denied, open 'npm-debug.log.3664877195']
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: 'npm-debug.log.3664877195'
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! Please include the following file with any support request:
npm ERR! /var/app/current/npm-debug.log
> cidermics@0.0.1 start /var/app/current
> node app.js
/var/app/current/node_modules/multer/node_modules/mkdirp/index.js:90
throw err0;
^
Error: EACCES: permission denied, mkdir '/var/app/public'
at Error (native)
at Object.fs.mkdirSync (fs.js:794:18)
at sync (/var/app/current/node_modules/multer/node_modules/mkdirp/index.js:71:13)
at Function.sync (/var/app/current/node_modules/multer/node_modules/mkdirp/index.js:77:24)
at new DiskStorage (/var/app/current/node_modules/multer/storage/disk.js:21:12)
at module.exports (/var/app/current/node_modules/multer/storage/disk.js:65:10)
at new Multer (/var/app/current/node_modules/multer/index.js:15:20)
at multer (/var/app/current/node_modules/multer/index.js:88:12)
at Object.<anonymous> (/var/app/current/routes/admin.js:8:14)
at Module._compile (module.js:409:26)
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm" "start"
npm ERR! node v4.4.6
npm ERR! npm v2.15.5
npm ERR! code ELIFECYCLE
npm ERR! cidermics@0.0.1 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cidermics@0.0.1 start script 'node app.js'.
npm ERR! This is most likely a problem with the cidermics package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node app.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs cidermics
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls cidermics
npm ERR! There is likely additional logging output above.
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/npm" "start"
npm ERR! node v4.4.6
npm ERR! npm v2.15.5
npm ERR! path npm-debug.log.1654992227
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall open
npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.1654992227'
npm ERR! at Error (native)
npm ERR! [Error: EACCES: permission denied, open 'npm-debug.log.1654992227']
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: 'npm-debug.log.1654992227'
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! Please include the following file with any support request:
npm ERR! /var/app/current/npm-debug.log
【问题讨论】:
你的这个8081端口在安全组里打开了吗? 【参考方案1】:您的依赖项multer
正在运行命令mkdir
。但是由于是node进程调用这个,并且node进程没有运行shell命令的权限,所以报错了。
multer
有多重要?您的代码 sn-ps 没有显示您使用它。也许你可以删除它?
这是related question。
【讨论】:
以上是关于Nodejs 502 Bad Gateway 在 Elastic Beanstalk AWS 上部署 Express的主要内容,如果未能解决你的问题,请参考以下文章
Nginx+NodeJS反向代理设置,出现502 Bad Gateway的解决办法
将 Lambda 运行时从 Node 8.x 更改为 Node 12.x 后,从 AWS API Gateway 随机获得 502 Bad Gateway 响应