How to run Two Node APPs in single digital ocean Droplet

 The link provided (https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04) shows pretty much everything you need to know about how to set it up.
The only difference, to make the two domains work your /etc/nginx/sites-available/default code would look something like this:

server {
    listen 80;

    server_name example1.com;

    location / {
        proxy_pass http://yourDropletAddress:8080;
        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;
    }
}

server {
    listen 80;

    server_name example2.com;
    location / {
        proxy_pass http://yourDropletAddress:8081;
        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;
    }   
}

For the DNS, you need to go to here, then Domains, and then input your domain and chosen droplet. Inside this change, you need to add the digital ocean nameservers and voala, it works!

I’m still looking for a more Node approach, but it is functional.

Edit1: this implementation, if you use your droplet IP on the proxy, makes it so you can still access a port, so you could still go to example1.com:8080. The solution with Node is easy:
Use the localhost as “yourDropletAddress”.
On your apps, in the app.listen command, specify the localhost as your hostname. That will make it so only the proxy can access those ports to redirect. Example usage:

var express = require("express");
var app = express();
var port = 8080;
//do all the routing and etc

var listener = app.listen(port, 'localhost', function() {
    console.log("Listening on port " + listener.address().port);
});

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

Related Articles
About Author