502 Bad Gateway Deploying an Express Generator Pattern on an Elastic Beanstalk

I used the express generator to create a simple express application that when running on dev works fine on localhost: 3000.

When I push this to an elastic beanstalk using the eb - git aws.push command, however, I get a 502 error on the production server.

Looking at the logs, I get the following message:

2014/04/01 19:29:40 [error] 24204#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.2.178, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "macenvexp-env-hqv9ucmzev.elasticbeanstalk.com" 2014/04/01 19:29:40 [error] 24204#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.2.178, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "macenvexp-env-hqv9ucmzev.elasticbeanstalk.com" 

I use the default nginx configuration. When I run the node.js sample application without Express, it works fine. Here's the express code in app.js:

 var express = require('express'); var http = require('http'); var path = require('path'); var favicon = require('static-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes'); var users = require('./routes/user'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(favicon()); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use(app.router); app.get('/', routes.index); app.get('/users', users.list); /// catch 404 and forwarding 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.render('error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.render('error', { message: err.message, error: {} }); }); module.exports = app; 

And here is the package.json file:

 { "name": "macEnvExp", "version": "0.0.1", "private": true, "scripts": { "start": "DEBUG=macEnvExp node bin/www" }, "dependencies": { "express": "~3.4.8", "static-favicon": "~1.0.0", "morgan": "~1.0.0", "cookie-parser": "~1.0.1", "body-parser": "~1.0.0", "debug": "~0.7.4", "jade": "~1.3.0" } } 

And here is bin / www:

 #!/usr/bin/env node var debug = require('debug')('my-application'); var app = require('../app'); app.configure(function(){ app.set('port', process.env.PORT || 3000); }); console.log(app.get('port')); var server = app.listen(app.get('port'), function() { debug('Express server listening on port ' + server.address().port); }); 
+57
amazon-web-services nginx express
Apr 01 '14 at 19:39
source share
8 answers

For clarity, I will state the answer from the comments.

AWS ELB runs node app.js BEFORE npm start . node app.js does not give an error, but it does not open any ports.

The solution is to simply rename app.js to everything except server.js (i.e. main.js ), and reference it in bin / www, pointing to it in the / bin / www file: var app = require('../app'); before var app = require('../main');

Then it should work correctly!




For clarity, here is what my directory looks like:

The package.json file will be called by ELB when the application server starts. Here it has instructions for starting the script node bin/www enter image description here

This is the bin/www file that is running. We see what is required ../main and app.set('port'...) enter image description here

Then the main.js file that starts the routing, and that’s it: enter image description here

When I created the project, the main.js file was named app.js The problem caused by this was based on the ELB priority seed sequences. ELB will launch the application and first check if app.js - if it exists, it starts node app.js , otherwise it checks if package.json exists and tries to start npm start . When main.js was named app.js , ELB tried to launch the entire application by starting it. However, this file does not open any ports.

+86
Aug 15 '14 at 18:04
source share
β€” -

An alternative to renaming app.js is to create an elastic beanstalk configuration file. Add the .config file to the .ebextensions folder, for example, .ebextensions/34.config . Change the NodeCommand parameter in the aws:elasticbeanstalk:container:nodejs to any command you want to run to start the server. For example, this is the minimum .config file to run npm start instead of app.js :

 option_settings: - namespace: aws:elasticbeanstalk:container:nodejs option_name: NodeCommand value: "npm start" 

See http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_custom_container.html and http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options.html#command for more details. -options-nodejs .

Edit: Even easier: Using the AWS console, Configuration / Software has the β€œNode” command - just set the npm start value.

+27
Jun 09 '15 at 21:41
source share

Set the working port to 8081

 app.set('port', 8081); 
+4
Dec 30 '16 at 21:39
source share

Actually there is one more option.

On the Elastic Beanstalk console, inside the app-environment section on the left side, there is the Configuration menu item (on the right side of the Dashboard menu). If you click there, you will find many configuration options. Click Software Configuration , and then specify which node command you have. Explain the order of the commands that he is really trying: "The command to start the Node.js application. If an empty string is specified, use app.js, then server.js, then" npm start "in that order"

My mistake was in my script launch command. He started nodemon:

 "scripts": { "start": "NODE_ENV=production && nodemon ./bin/www" 

Then I switched to node and it worked:

 "scripts": { "start": "NODE_ENV=production && node ./bin/www" 

Hope I helped someone.

+3
Jul 16 '16 at 12:11
source share

If you use port 8081 to start your express application and use sudo to start the host server, your application will be accessed directly from the URL of the elastic bean without port numbers, otherwise, 502 Gateway error will be displayed on nginx.

Nginx proxying 8081 port by default for node app on elastibeanstalk.

Create a file: .ebextensions/nodecommand.config and set the following parameters:

 option_settings: aws:elasticbeanstalk:container:nodejs: NodeCommand: sudo pm2 start server.js (server command with sudo ie. sudo node /bin/www) 

You can create another file for container commands: .ebextensions/01_init.config and place the necessary commands that will be executed before deployment. For example:

 container_commands: 01_node_v6_install: command: sudo curl --silent --location https://rpm.nodesource.com/setup_6.x | bash - 02_install_node: command: sudo yum -y install nodejs 03_npm_install_gulp_webpack: command: sudo npm install -g gulp webpack pm2 04_npm_install: command: sudo npm install 05_webpack_run: command: sudo webpack 
+3
Dec 09 '16 at 11:07
source share

In case someone did the stupid thing I did, make sure your bin folder is fixed if you use express. I had my .gitignore file and that is why I got the 502 error.

Just remove /bin from .gitignore , confirm the changes and make the changes to EB.

+2
Jul 25 '15 at 16:38
source share

I am new to AWS, and some time ago I was online, but got stuck on the same issue tonight, and thanks to everyone in the chain, I am very happy to say that the basic lesson on socket.io now works like a miracle, I just forgot one line in package.json:

 "scripts": { "start": "node app.js" } 

oh and port! the only thing I saved in the samplebean sample node.js application is the value instead of the pure 3000 value:

 var port = process.env.PORT || 3000; 
+1
Aug 31 '18 at 23:45
source share

Note. I ran into this problem and none of the solutions helped me.

My solution was to make sure that the devDependencies in package.json are indeed in the dependencies.

For example:

 { "name": "whaler-test", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www", "create-db": "cd dynamodb && node createDonorsTable.js && cd ..", "delete-db": "cd dynamodb && node deleteDonorsTable.js && cd ..", "load-data": "cd dynamodb && node loadDonorsData.js && cd ..", "read-data": "cd dynamodb && node readDataTest.js && cd .." }, "dependencies": { "cookie-parser": "~1.4.3", "debug": "~2.6.9", "express": "~4.16.0", "http-errors": "~1.6.2", "jade": "~1.11.0", "morgan": "~1.9.0", "nodemon": "1.17.5", "cors": "2.8.4", "aws-sdk": "^2.270.1" } } 

Not:

 { "name": "whaler-test", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www", "create-db": "cd dynamodb && node createDonorsTable.js && cd ..", "delete-db": "cd dynamodb && node deleteDonorsTable.js && cd ..", "load-data": "cd dynamodb && node loadDonorsData.js && cd ..", "read-data": "cd dynamodb && node readDataTest.js && cd .." }, "dependencies": { "cookie-parser": "~1.4.3", "debug": "~2.6.9", "express": "~4.16.0", "http-errors": "~1.6.2", "jade": "~1.11.0", "morgan": "~1.9.0", "nodemon": "1.17.5" }, devDependencies { "cors": "2.8.4", "aws-sdk": "^2.270.1" } } 
0
Jul 09 '18 at 8:26
source share



All Articles