You’ve probably made several apps in Node.js, perhaps via Express, and now you want to show them to the world. The URL to your app is yourdomain.com:3000, but this looks quite ugly with the port number in the end. To solve this, you may have tried to make the HTTP server listen to port 80, but it needs root access and it does not feel right.
I know many of you already have Apache2 running on Linux, so I’ve made a recipe for you on how to fix this problem in a better way. The trick is to make a proxy so Apache2 redirects things on port 80 to and from your Node.js app port. For example: if your app runs on yourdomain.com:3000, the proxy will make it available via yourdomain.com. You can also proxy sub-urls such as yourdomain.com/myapp to another Node.js app.
Start off with enabling the needed Apache2 module, mod_proxy:
sudo a2enmod proxy
I assume that you’ve already have set up a virtual host for port 80 and enabled it. If not, then please do.
To create the proxy, add the following code to your virtual host site file (somewhere inside the <VirtualHost> element):
ProxyPass / http://yourdomain.com:3000
That’s it! Restart apache2 and you should be able to access the app via http://yourdomain.com/.
To proxy things to and from some sub-urls, do this:
ProxyPass /myapp http://yourdomain.com:3000
If you get errors about the proxy module (see the apache2 error log), then you may want to look at this blog post.
To learn more about reverse proxies and how they work, see this.