6

I'm stuck trying to setup up several Node apps on different domains on one Digital Ocean 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 Nginx settings like it asked:

I uncommented the server_names_hash_bucket_size 64; (like it says)

I created /etc/nginx/conf.d/example.com.conf files for the apps (they are different domains. I put 1 on port 3000 and the other on 4000).

example:

server {
listen 80;

server_name your-domain.com;

location / {
    proxy_pass http://localhost:{YOUR_PORT};
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

}

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?

I can't seem to get more than 1 app running at once. I can figure out how to properly assign a Node app folder to a port and keep it alive forever with forever.

1
  • I was hitting my head against the wall for a while on this and changed the proxy_pass http://localhost:{PORT} to http://127.0.0.1:{PORT} and it worked. Not sure why nginx wasn't reading the localhost but yeah...in case anyone else runs into something similar.
    – Richard
    Dec 16, 2015 at 22:39

2 Answers 2

6

Like I mentioned over on DigitalOcean, 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.

1
  • Thanks, Andrew. That does clear up some things. Let me tinker with it some more and see if I can figure it out. May 7, 2014 at 23:55
3

See what you have to do is: Lets say you have 3 node instances running on 3000,5000,7000. Now you have to point 3 sub domains to same ip lets say ig you have a domain example.com then ex1,ex2,ex3 these three will point to same ip. Now create 3 seperate files in /etc/nginx/sites-enabled/ lets say ex1.example.com, ex2.example.com, ex3.example.com now configure the server blocks in these files to oint to respective node application and restart nginx. Now you have three node applications on same server with three different access links..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.