How do I host 3 node apps with 3 different domains on one droplet?

May 7, 2014 3.1k views
Hello all. Stuck trying to setup up several Node apps on one droplet. I followed the Host Multiple Node.js Applications On a Single VPS with nginx, forever, and crontab article exactly. I have the domains all pointed correctly and A records set. I can't seem to get apps to run (with forever) on any other port besides the default express 3000. I changed the server_names_hash_bucket_size 64; (like it says) I created /etc/nginx/conf.d/ for the apps (they are different domains. I put 1 on port 3000 and the other on 4000) I don't understand the difference between when Nginx is running the app and when forever is? Where does "npm start" come into play? How many potential servers are working at the same time?
2 Answers

check out [] or [] if you want to do it the NodeJs/Express way.
assume port 8001 is node app A and 8002 is node app B.
change to port 80 and nodemon this file.

var bouncy = require('bouncy');

var server = bouncy(function (req, res, bounce) {
if ( === '') {
else if ( === '') {
else {
res.statusCode = 404;
res.end('no such host');

Nginx doesn't run the app. It is a webserver and it acts as a proxy in this case. The apps are run on a non-standard port and Nginx than relays then to port 80 on the correct domains.

Selecting the port to run on, is up to the app itself. Are these apps you've written or just things you've installed? Usually, you can set the port as an environment variable. So, you'd start your app with something like:

PORT= 4000 forever start --sourceDir /path/to/your/node/app main.js

That assumes that the app does something like the bellow. This is from the template created using express-generator, so it is fairly universal:

#!/usr/bin/env node
var debug = require('debug')('my-application');
var app = require('../app');

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);

That defaults to using port 3000 unless the environment variable is set.
Have another answer? Share your knowledge.