How to use vhosts alongside node-http-proxy?

I never figured out Haibu or Cluster. But I did find a good solution that addressed my issue. To my surprise, it was actually quite simple. However, I don’t know much about servers, so while this works, it may not be optimal.

I set up virtual hosts like normal on Apache
(http://httpd.apache.org/docs/2.0/vhosts/examples.html)

I installed the following on Node

  • Express (http://expressjs.com/)
  • node-http-proxy (https://github.com/nodejitsu/node-http-proxy)

Then, as a matter of personal style, I placed all my virtual hosts in a common directory (/localhost)

I then switched Apache to listen on a port other than port 80. I just happened to choose port 9000 because I had seen that used somewhere. (In httpd.conf, changed “Listen 80” to “Listen 9000”). I also had to make sure that all my virtual hosts, as defined in extra/httpd-vhosts.conf were set to an IP based nameVirtualHost (127.0.0.1) instead of using a port (*:80).

On the Node side, I created my app/server (aka node virtual host) that listened on port 8000 (somewhat arbitrarily choice of port number) See this link on creating a server with express: http://expressjs.com/guide.html

In my /localhost directory I then created a file called “nodeHttpProxy.js”

Using node-http-proxy, in nodeHttpProxy.js I then created a proxy server that listens on port 80. Using express, which wraps connect (http://www.senchalabs.org/connect/) I created my virtual hosts.

The nodeHttpProxy.js file looks like this:

// Module dependancies
var httpProxy = require('/usr/local/lib/node_modules/http-proxy/lib/node-http-proxy')
, express = require('/usr/local/lib/node_modules/express/lib/express');

// Http proxy-server
httpProxy.createServer(function (req, res, proxy) {

    // Array of node host names
    var nodeVhosts = [
        'vhost1'
        , 'vhost2'
    ]
    , host = req.header('host')
    , port = nodeVhosts.indexOf(host) > -1
        ? 8000
        : 9000;

    // Now proxy the request
    proxy.proxyRequest(req, res, {
        host: host
        , port: port
    });
})
.listen(80);

// Vhosts server
express.createServer()
.use(express.vhost('vhost1', require('./vhost1/app')))
.use(express.vhost('vhost2', require('./vhost2/app')))
.app.listen(8000);

As you can see, I will have to do two things each time I create a new Node virtual host:

  1. add the virtual host name to my “nodeVhosts” array
  2. define a new express virtual host using the .set method

Of course, I will also have to create the actual host path/files in my /localhost directory.

Once all this is done I just need to run nodeHttpProxy.js:

node nodeHttpProxy.js

You might get some weird “EACCESS” error, in which case, just run as sudo.

It will listen on port 80, and if the host matches one of the names in the nodeVhosts array it will forward the request to that host on port 8000, otherwise it will forward the the request onto that host on port 9000.

Leave a Comment