How can I create a Sub Domain that links to NodeJS application?

December 23, 2014 3.7k views

I have recently created a NodeJS application and am trying to get a subdomain to point to it. My Digital Ocean DNS is fine and is directing to my server properly. My Apache virtual host is allowing the connection, however it sends back my default document root, not the data that is being served on the specified port. I fear I may be doing it wrong. Could someone provide me with steps on how to make Apache point to a port on my server?


3 Answers

A standard A or CNAME record can only direct a subdomain at a name or IP address but cannot specify a port. SRV records will allow you to specify a port but may not be supported in all instances. The best course of action to host a node.js application on a subdomain would be to set it up on a droplet with it's own IP and point your subdomain to that.

Another option would be configuring Apache to reverse proxy requests to NodeJS. Create a VirtualHost that looks like this:

<VirtualHost *:80>

    <Location "/">
        ProxyPreserveHost On
        ProxyPass http://localhost:3000/
        ProxyPassReverse http://localhost:3000/

Replace 3000 with the port your nodejs app is listening on and and with the actual domain name you want to serve your nodejs app on.

Then, restart Apache:

sudo service apache2 restart
by Justin Ellingwood
The Apache web server is the most popular way to serve web content on the internet. Apache has the ability to serve multiple domains from a single server by using a mechanism called "virtual hosts". If a virtual host is configured correctly for each domain, the web server can correctly route traffic to the appropriate files based on the domain name requested. In this guide, we'll demonstrate how to configure Apache virtual hosts on an Ubuntu 14.04 VPS.

I'm not sure exactly how you went about creating and deploying your app, but take a look at the post I just wrote here: - You may be able to abstract some of the deployment steps to help you out.

Have another answer? Share your knowledge.