Configure Sails.js with Subdomains on Ubuntu


I have been learning node.js lately, and I have become particularly fond of the sails.js framework for spinning up quick MVC websites. I will probably do a series of posts on sails in the coming weeks, but for now I just wanted to get this small nugget of information out there for anyone that might need it.

Many people (myself included) host several sites from the same computer. In my case this computer is a hosted virtual ubuntu box at dreamhost’s data center. This means that if I wanted to spin up another VM it would cost me another $XX.99  a month, so I clearly want to keep my machine count down.

Traditionally, I would be using apache and it would be is easy to map domains to folders under /var/www. With node & sails however, apache is completely out of the picture. There are fortunately a number of libraries for node.js that allow virtual hosts, unfortunately for me, they require raw express, and do not play nicely with sailsjs.

The solution is nginx (“engine x”) . Nginx is actually a rather robust package that can act as a full replacement to apache. But, unlike apache, it is modular and lightweight and stays out of the way when not needed. For our purposes we will only be using it as a reverse proxy.

The basic plan will be to have multiple instances of sails.js all listening on localhost but at different ports. Nginx will monitor incoming requests, check their domain and reroute them localhost:PORT where PORT is the port that the appropriate instance of sails is listening on. As illustrated in the diagram below.

nginx sails

Ok, lets make it happen.

Step 1. Set Ports on Sails.js
Sails has numerous places where you can make this change, and which one you choose will be contingent upon what environment you are in.

If you are in production and you are lifting sails with –prod (or have NODE_ENV set to prod) then it will be in config>env>production.js

If you are in development it will be in config>env>development.js

If you are a wild cowboy and hate the internet you can put it in config>local.js

Either way the steps are the same, open the appropriate file, and specify the port as illustrated below  (for the chat sails instance).

module.exports = {
//your existing settings,

port: 5003,
};

You would want to repeat this process in each of your sails installations. In our example we have three.

Step 2. Install Nginx
I am using ubuntu, and it already had nginx installed, but I like to duplicate effort so I reinstalled it!

sudo apt-get update
sudo apt-get install nginx

Step 3. Configure Nginx
Nginx will put a couple of config files on your machine by default. You can put the configuration in almost any of them, but the most logical place from my experience is /etc/nginx/sites-enabled/default. If you are using nginx as a web server for regular content in addition to a reverse proxy then you need to be a little more careful, but for our purposes, you can delete everything in this file.

I want to host three sub-domains from this box, as illustrated int he diagram above, so I would need to make the following entries:

server {
        listen 80;
        server_name example.com;

        location / {
                proxy_pass http://localhost:5001;
        }
}

server {
        listen 80;
        server_name admin.example.com;

        location / {
                proxy_pass http://localhost:5002;
        }
}

server {
        listen 80;
        server_name chat.example.com;

        location / {
                proxy_pass http://localhost:5003;
        }
}

As you can see we make a server entry per sub-domain, specify the incoming port and host name (this host name needs to actually point to this machine via DNS entry of course). And then finally specify the root location for requests made to this sub-domain, remembering to specify the appropriate port for each sails.js instance.

Now restart nginx:

sudo service nginx restart

Step 4. Start Your Sails.js Instances

I personally use a script to start and stop all my sails.js instances, but you could just as easily cd to each sails app’s root folder and lift it manually to the background.

sails lift &

That’t it! 4 simple steps to set up reverse proxy and sails to use one machine to listen to multiple sub domains.

One comment on “Configure Sails.js with Subdomains on Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *