This question was asked on Stackoverflow. Several answers which involved using some sort of proxy or reverse proxy in nginx, including this one from Digital Ocean were given. I prefer to do without having to install either Apache or nginx so here is how I do it.
First create a script that would run on startup:
Save this script to /etc/init.d or create a symbolic link in /etc/init.d that points to that script.
$: cd /etc/init.d
$: sudo ln -s ~/startup/node-apps-startup.sh node-apps
$: sudo update-rc.d node-apps defaults
$: sudo update-rc.d node-apps enable
Make it executable
$: chmod +x node-apps-startup.sh
$: npm install -g forever
$: npm install -g bouncy
In the bouncy directory, create a file called routes.json like this
“app1.domain.com" : 5000,
“app2.domain.com" : 5001
As I commented inline in the startup script above, the iptables command routes all traffic from port 80 to port 8000 where bouncy runs. There are many guides and tutorials on iptables which is a fairly powerful and complicated firewall tool, but for me this one guide seems to make it easy for me to understand. Since this involves port 80, a sudo elevation level is required. bouncy then based on its routes.json mapping, pipes traffic to the right endpoints. So I can easily keep track of all of my Node.js applications, I randomly pick the number 5000 and let this be the starting port where my app1, app2, etc run on.
In the above example, I have app1.domain.com and app2.domain.com subdomains A records created with my DNS management provider, and they all point to the same IP address of my VPS server.
Lastly, here are a couple of applications that I’m currently working on but prove that the above approach does work: