kyozeus
By:
kyozeus

Two different Node apps, with two different domains, in one droplet.

July 9, 2016 4.2k views
Node.js MEAN DNS Networking DigitalOcean Ubuntu

Ok, for instance, I have two apps running on two different ports, using forever. Using a third app, how could you make example1.com go to 127.0.0.1:3000 and example2.com go to 127.0.0.1:3001?
I've seen answers talking about vhosts, but exclusively using nginx, and what should I do, especially regarding DNS?

PS: most answers tend to be using subdomains, which isn't the case. It's two completely unrelated domains. Also, both apps have very low visitations, so having multiple apps hosted on the same droplet shouldn't be a problem.

2 Answers
kyozeus July 10, 2016
Accepted Answer

Ok, this is the solution using nginx:
as @jtittle mentioned, you will set up a server to act as proxy. 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.
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 digitalOcean 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 localhost as "yourDropletAddress".
On your apps, in the app.listen command, specify 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);
});

@kyozeus

Using NGINX, please take a look at the guide below and search for the heading "Set Up Reverse Proxy Server", which will provide you with exactly what you need to setup multiple domains using NGINX, which will proxy requests to your NodeJS application.

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

Essentially NGINX will handle the initial request on Port 80 and then proxy it over to the port used by your NodeJS application.

Node.js is an open source Javascript runtime environment for easily building server-side and networking applications. The platform runs on Linux, OS X, FreeBSD, and Windows, and its applications are written in JavaScript. Node.js applications can be run at the command line but we will...
  • I'm going to try, but from the overlook, it uses a single domain, but i'm sure it is possible with nginx. I'll report my results once i'm finished, but if anyone has a node solution (and DNS instructions), feel free to complement.

Have another answer? Share your knowledge.