node.js - Nodejs 502 Bad Gateway 在 Elastic Beanstalk AWS 上部署 Express

标签 node.js amazon-web-services nginx amazon-ec2 amazon-elastic-beanstalk

我将 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 (这是同样的问题) 但它不适用于我的代码。(我尝试了大多数选择的重命名解决方案) 虽然我阅读了 StackOverflow 上的其他资源,这些资源建议我将我的主文件从 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

最佳答案

您的依赖项 multer 正在运行命令 mkdir。但是由于它是调用它的 Node 进程,并且 Node 进程没有运行 shell 命令的权限,所以它会抛出错误。

multer 有多重要?您的代码片段没有显示您在使用它。也许你可以删除它?

这是一个related question .

关于node.js - Nodejs 502 Bad Gateway 在 Elastic Beanstalk AWS 上部署 Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38931510/

相关文章:

node.js - Pub/Sub Redis - NodeJS 服务器之间的通信

javascript - 检查 Discord 服务器中是否存在用户 ID

node.js - 用于生产的 Kibana 4 配置

node.js - Mongoose 试图打开未关闭的连接

amazon-web-services - 如何获取Cloudwatch中所有ECS实例的日志?

amazon-web-services - AWS cloudformation 验证模板不断给出错误(模板格式错误)

amazon-web-services - 亚马逊托管无法安装.Ca-bundle

php - Nginx:将子域重写为其中的文件夹和文件

ruby-on-rails - Nginx 乘客集成模式总是需要 Passengerfile.json 文件?

docker - 在docker下的nginx找不到文件error.log和access.log