Two different Node apps on sub domains in a single droplet.

September 16, 2017 633 views
Node.js Nginx Deployment Ubuntu 16.04

I want to run two different apps on two different sub domains under one droplet.

1 Answer

In order to run two Node apps on a single Droplet, you'll need to configure a reverse proxy to direct requests to the correct applications. Nginx is a popular choice for doing this. Each app will need to be configured to listen on a separate port locally while Nginx will listen publicly with a "server block" for each app. A quick minimal example of the Nginx configuration would look like:

server {
    listen 80;

    server_name subdomain1.example.com;

    location / {
        proxy_pass http://localhost:PORT_ONE;
        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 subdomain2.example.com;

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

Depending on the details of your applications, there may be additional configuration needed. Check out these tutorials for more information. The first covers Nginx server blocks; the second is specifically on using Nginx as a reverse proxy for Node apps:

by Brennen Bearnes
Node.js is an open source JavaScript runtime environment for easily building server-side and networking applications. Node.js applications can be run at the command line but this guide focuses on running them as a service using PM2, so that they will automatically restart on reboot or failure, and can safely be used in a production environment.
Have another answer? Share your knowledge.