Nodejs 502 Bad Gateway在Elastic Beanstalk AWS上部署Express

时间:2016-08-13 09:54:45

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

我将节点js应用程序部署到AWS EBS。当我运行应用程序时,我收到错误“502 Bad Gateway”nginx / 1.6.2。这是我在日志中发现的。

  lhs: false
  rhs: true

我为此花了一天时间。我发现了一些很好的解决方案。 enter link description here(同样的问题) 但它不能解决我的代码问题。(我尝试了最多选择的重命名解决方案) 虽然我已经阅读了StackOverflow上的其他资源,建议我将我的主文件从app.js重命名为main.js并将端口设置在bin / www文件夹中,我觉得这不是我的应用程序的解决方案。

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/"

这是main.js我将名称从app.js更改为main.js

{
  "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" : "*"
  }
}

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);
});

1 个答案:

答案 0 :(得分:0)

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

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

这是related question