ExpressJS - Elastic Beanstalk 502 Bad Gateway
Solution 1
I had the same problem tonight. It turned out that the node app had not been started even though the portal claims that it will run npm start
by default.
Here is what I did to fix it:
- Create a directory named .ebextensions in the root of my project
- Inside .ebextensions create a file named nodecommand.config
- Inside the nodecommand.config add the following yaml:
option_settings: - namespace: aws:elasticbeanstalk:container:nodejs option_name: NodeCommand value: "npm start"
The full instructions are available here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html
Solution 2
-
Add
"scripts": { "start": "node app.js" }
to yourpackage.json
. If your main js file is notapp.js
, don't forget to rename it! :) -
In Beanstalk console: Configuration->Software add new env var:
port: 8081 (PORT: 8081)
Important: even if you add env var as port
you still have to use PORT
(uppercase). That is what solved my problem.
I tried to use process.env.port
, but it did not work.
Use process.env.PORT
instead.
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log("Server is listening on: ", port);
});
Solution 3
From the EB App Dashboard, go to Configuration -> Software Configuration -> add value "npm start", or "node 'yourmainfile'" to the Node Command.
Even though AWS shows "Command to start the Node.js application. If an empty string is specified, app.js is used, then server.js, then "npm start" in that order", it looks like it doesn't run unless you specify.
Solution 4
In my case the issue was my port, my express server was listening on port 80. I just needed to change it to 8081 because i think the EB reverse proxy (nginx/apache) tries to redirect traffic to 8081 by default.
I figured it out by checking the AWS EB access logs.
Solution 5
In my case it was process.env.port
in index.js. The port was in a small case. I modified it to process.env.PORT
Related videos on Youtube
cphill
Updated on September 19, 2021Comments
-
cphill over 2 years
I am running a Express.js app on Amazon Elastic Beanstalk and my environment was successfully created, but when I go to my environment via the URL Amazon has created, I receive a
502 Bad Gateway nginx/1.6.2
error. While I have read other resources on StackOverflow, which suggest I rename my main file fromserver.js
tomain.js
and set theport
in abin/www
folder, I feel like this isn't the solution for my app. I rannode server.js
, which is the command I believe Elastic Beanstalk runs and it doesn't work, (error message):node server.js events.js:72 throw er; // Unhandled 'error' event ^ Error: failed to connect to [undefined:27017]
because I have ENV variables set up in a
.env
file, so the only way I can run my server is with foreman. This has made me think that the 502 error is the result of Elastic Beanstalk not being able to comprehend the variable and thus causing the server to fail. Can anyone confirm that I am on the right path or is this issue really because my main file is namedserver.js
and not in thebin/www
folder?Here is my code from my
server.js
file://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);
and my package file:
{ "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": "*" } }
-
Noman Ur Rehman about 9 yearsYou are running the nodejs app behind an nginx proxy ? Have you setup the correct reverse proxy configuration ?
-
Raeesaa almost 9 years@cphill: Looking at your logs, it seems that deployed application is not getting right mongoDB path
Error: failed to connect to [undefined:27017]
. Did you try setting upNODE_ENV
to right value from EBS console? -
Zach Saucier almost 6 yearsPossible duplicate of 502 Bad Gateway Deploying Express Generator Template on Elastic Beanstalk
-
-
Zach Saucier almost 6 yearsThis doesn't fix the issue for me
-
Bira over 5 yearsAwesome! fixed the issue
-
Giwoo Gustavo Lee about 5 yearsYou can also check logs with
eb logs
for possible errors that happened while deploying your NodeJS application. EB sort of bypasses certain errors and still says it "successfully deployed" the application. -
KeitelDOG over 4 years@GiwooGustavoLee with Logs I was able to see error from Nginx as a Proxy Server trying to forward request to
IP:8081
. SO I change Node Port to 8081 -
Yogeshwar Tanwar over 2 yearsthis works for me