Nodejs: want to map domains to server

I have a webapp where I allow people to create sub-sites on my app server (ex:mysite.com/their-site). I want them to be able to point their domain to the sub-site on my app. Similar to how you attach a domain to a shared hosting server.

I'm not really sure how to go about this. Would appreciate it if anybody could enlighten me.

If you use something like node http-proxy or nginx you can run each of the separate sites on different local ports and then map the domain name back. I use the http proxy but others love nginx.

For that to work, you'll need to add DNS entries resolving to the IP address that your reverse proxy is running on. Then for example, the port 80 request will get mapped back to localhost:3000.

This allows for each app to run in isolation and in separate processes from the other sites. They can't crash each other. You also don't have namespace issues where both apps claim the same path.

My app.js for the http proxy looks like this:

var proxyPort = 80;
var http = require('http');
var httpProxy = require('http-proxy');

var options = {
    router: {
        'localhost': '127.0.0.1:3000',
        'somesite.com': '127.0.0.1:3000',
        'othersite.com': '127.0.0.1:4000'
    }
};
console.log('Proxy Routing:')
console.log(options);var options = {
console.log();

var proxyServer = httpProxy.createServer(options);
proxyServer.listen(proxyPort);
console.log('Proxy listening on port ' + proxyPort);

You can also route with paths. See this: https://github.com/nodejitsu/node-http-proxy/issues/232

So more like your original request, you can do:

router: {
    'myhost.com/appA': '127.0.0.1:8080',
    'myhost.com/appB': '127.0.0.1:9090/appB',
    'myhost.com/appC': '127.0.0.1:9090/appC'
  }
};

Another option is to simply provide a route/page per customer and then it's simply a DNS problem. You would setup DNS so that all domain names resolve to the one IP address your app runs on. That has some downsides:

  1. domain1.com/user1, domain1.com/user2, domain2.com/user1 and domain2.com/user2 would all work which you probably don't want.
  2. Do you control all the code or do they supply code? If it's user supplied code then you want some isolation so therefore separate apps.
  3. You still likely need an http proxy to run on port 80 in linux and not run as sudo.