ExpressJS - Elastic Beanstalk 502 错误网关

ExpressJS - Elastic Beanstalk 502 Bad Gateway

我正在 运行在 Amazon Elastic Beanstalk 上安装一个 Express.js 应用程序并且我的环境已成功创建,但是当我通过 URL Amazon 创建的环境进入我的环境时,我收到 502 Bad Gateway nginx/1.6.2 错误。虽然我阅读了 Whosebug 上的其他资源,建议我将主文件从 server.js 重命名为 main.js 并将 port 设置在 bin/www 文件夹中,但我觉得这不是' 我的应用程序的解决方案。我运行node server.js,这是我相信的命令 Elastic Beanstalk 运行s 但它不起作用,(错误消息):

node server.js
events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: failed to connect to [undefined:27017]

因为我在 .env 文件中设置了 ENV 变量,所以我可以 运行 我的服务器的唯一方法是使用工头。这让我认为 502 错误是 Elastic Beanstalk 无法理解变量从而导致服务器失败的结果。任何人都可以确认我在正确的道路上还是这个问题真的是因为我的主文件名为 server.js 而不是在 bin/www 文件夹中?

这是我的 server.js 文件中的代码:

//Load express
var express = require('express');
var app = express();
var router = express.Router(); // get an instance of the router
var bodyParser = require('body-parser'); // configure app to use bodyParser()
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var aws = require('aws-sdk');

app.use(bodyParser.urlencoded({ extended: true})); // get data from a POST method
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(cookieParser());


var port = process.env.PORT || 8080; // set the port

var DB_CONFIG = process.env.DB_CONFIGURATION;
var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
var S3_BUCKET = process.env.S3_BUCKET;

var blogDB = require('./config/blogDB.js');
mongoose.connect(blogDB.url);




require('./config/passport.js')(passport);


app.set('view engine', 'ejs'); // set ejs as the view engine

app.use(express.static(__dirname + '/public')); // set the public directory

app.use(session({ secret: 'thisisatest' }));
app.use(passport.initialize());
app.use(passport.session());

app.use(flash());


var routes = require('./app/routes');

app.use(routes); // use routes.js


app.listen(port);
console.log('magic is happening on port' + port);

和我的包文件:

{
  "name": "test",
  "main": "server.js",
  "dependencies": {
    "body-parser": "1.6.5",
    "ejs": "^1.0.0",
    "express": "^4.6.1",
    "express-paginate": "0.0.2",
    "mongoose": "~3.6.15",
    "mongoose-paginate": "^3.1.0",
    "serve-favicon": "*",
    "passport" : "~0.1.17",         
    "passport-local" : "~0.1.6",
    "connect-flash" : "~0.1.1",     
    "bcrypt-nodejs" : "latest",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.0",
    "method-override": "~1.0.0",
    "express-session": "~1.0.0",
    "aws-sdk": "*" 
  }
}

今晚我遇到了同样的问题。事实证明,尽管门户网站声称默认情况下它将 运行 npm start

,但节点应用程序尚未启动。

这是我修复它的方法:

  1. 在我的项目的根目录中创建一个名为 .ebextensions 的目录
  2. 在 .ebextensions 中创建一个名为 nodecommand.config
  3. 的文件
  4. 在 nodecommand.config 中添加以下 yaml:
option_settings:
  - namespace: aws:elasticbeanstalk:container:nodejs
    option_name: NodeCommand
    value: "npm start"

完整说明可在此处获得:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html

我也遇到过这个问题,但这是因为 npm module 中缺少一个。您可以在 elastic beantalk 仪表板的日志部分看到缺少的 npm model 名称。它在我的本地机器上运行良好,只是因为那个特定的 npm 模块全局安装在我的系统中。

从 EB 应用程序仪表板,转到配置 -> 软件配置 -> 添加值 "npm start",或 "node 'yourmainfile'" 到节点命令。

即使 AWS 显示“启动 Node.js 应用程序的命令。如果指定空字符串,则使用 app.js,然后使用 server.js,然后在 "npm start"那个订单”,看起来它没有 运行 除非你指定。

对于我的情况,我分享了对错误 502 Bad Gateway Nginx 有用的方法。

AWS Elastic Beanstalk 正在使用 Nginx 作为负载平衡的代理服务器,即使您刚刚开始也是如此。

我从 AWS EB 控制台管理去寻找来自 Nginx 的错误日志。始终检查日志,因为您的错误可能与其他错误不同。

现在假设您的 IP 是 AAA.BBB.CCC.DDD。在报错中发现,Nginx收到了来自AAA.BBB.CCC.DDD的请求,并转发给了AAA.BBB.CCC.DDD:8081。由于我在我的代码中启动了 Node to port 3003,所以我不得不为生产环境或任何你称之为 AWS EB 服务器环境的环境更改它。

如果需要,您可以使用 ENV 来保持灵活性(ENV 可以直接添加到 EB 控制台)。一旦您将 PORT 设置为 8081,请确保您提交,因为 Elastic Beanstalk 遵循 GIT 规则来知道要获取哪个文件,如果您不提交,它将不会部署更改。

完成后,使用命令 eb deploy 并观察它现在的运行情况。

对我来说,错误是因为我没有在软件配置部分设置 "npm start",即使我在创建应用程序之前已经设置了。 似乎有一些错误,我不得不重新设置它所以请检查它是否设置!

在我的例子中,问题出在我的端口上,我的快速服务器正在侦听端口 80。 我只需要将它更改为 8081,因为我认为 EB 反向代理 (nginx/apache) 默认会尝试将流量重定向到 8081。

我通过检查 AWS EB 访问日志弄明白了。

晚上都

根据我的经验,当您 运行 Express 不在正确的端口时,您通常会收到此错误。

Nginx 尝试将流量从端口 8081 路由到服务器,这可能是负载平衡的。

因此,您应该明确确保使用端口 8081 作为您的 Express 监听端口(例如 process.env.PORT=8081)

谢谢

基顿

克隆后我忘记了 运行 npm install,sudo npm install 对我有用。

确保 package.json 文件中的脚本部分如下所示:

"scripts": {
     //whatever other scripts

     "start": "node app.js",

     //whatever other scripts
},

请注意,NodeStart 现在被视为“旧版”:

You can add a Procfile to your source bundle to specify the command that starts your application, as the following example shows. This feature replaces the legacy NodeCommand option in the aws:elasticbeanstalk:container:nodejs namespace.

同一份文件(来源如下)指出:

When you don't provide a Procfile, Elastic Beanstalk runs npm start if you provide a package.json file. If you don't provide that either, Elastic Beanstalk looks for the file app.js or server.js, in this order, and runs it.

来源:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs.container.html

  1. "scripts": { "start": "node app.js" } 添加到您的 package.json。 如果你的主js文件不是app.js,别忘了重命名! :)

  2. 在 Beanstalk 控制台中:配置->软件添加新环境变量:port: 8081 (PORT: 8081)

重要提示: 即使您将环境变量添加为 port,您仍然必须使用 PORT(大写)。这就是解决我的问题的原因。 我尝试使用 process.env.port,但没有用。 请改用 process.env.PORT

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log("Server is listening on: ", port);
});

在我的例子中是 process.env。port 在 index.js 中。港口在一个小箱子里。我修改为process.env.PORT