My node-reverse-proxy is both stable and public

Sunday, 20 March 2011

I posted a brief snippet of code on Friday which was my initial stab at a reverse HTTP proxy in Javascript (using node.js).

Over the past couple of days I've tidied it up, added a command line parser, and made it flexible enough that it works for me.

My node reverse HTTP proxy is now both documented ( a little ) and available for further eyeballs.

Usage is pretty much:

$ node ./node-reverse-proxy.js --config ./path/to/config.file.js

The configuration file defines lists of virtual hosts along with the destination back-ends to proxy to - which is usually going to be a server running upon a high port on the loopback adapter, but might not be.

In addition to that we can perform rewrites such as:

  * Handler for wildcard host: *
         * Rewrites for static files - these will be handled via a
         * separate virtual host.
        'rules': {
            '^/robots.txt':  '',
            '^/favicon.ico': '',

That says requests for will be redirected to

Alternatively we can invoke javascript for each request matching a pattern:

     * will mostly proxy to
     * but files beneath /private/ have an IP-based ACL.
        host: 'localhost',
        port: '1008',

        'functions': {
            '/private': (function(orig_host, vhost,req,res) {
                var remote = req.connection.remoteAddress;;

                if ( ( remote != "" ) &&
                     ( remote != "" ) &&
                     ( remote != "") &&
                     ( remote != "" ) )
                    res.write( "Denied access to " + req.url  + " from " + remote );

Fun stuff. It was live for my server, replacing apache, for a few hours today. I need to add some trivial HTTP Basic-Auth handling then it will go back.

Otherwise I hope it is vaguely useful to others, and that the provided examples explain things neatly.

ObQuote: "Only one thing alive with less than four legs can hear this frequency" - Superman.



Comments On This Entry

[gravitar] Bob

Submitted at 14:10:52 on 20 march 2011


I tried node.js recently, but I had problem with the packaged version in Debian: some of the examples from didn't worked well...

Is there a recommended version for this reverse proxy? Did you use the debian package? Did you compile it from sources?


[author] Steve Kemp

Submitted at 16:10:43 on 20 march 2011

Because my server, and my desktop where I develop, is running the Squeeze version of Debian there is no nodejs package availabe.

The Debian nodejs package is only available for Wheezy/Unstable.

So I backported nodejs for Squeeze, and haven't run into any problems with it at all - it has successfully run both my own code and random samples of things I've attempted to run against it.

[gravitar] Justin Randell

Submitted at 04:43:07 on 21 march 2011

very nice. did you look at:

not sure it supports rewrite rules, but i guess it would support patches to do so.

[author] Steve Kemp

Submitted at 11:10:28 on 21 march 2011

No I didn't, though it looks like it does similar things when hooked up with the "proxytable".

Looks like mine is best for having rewrite facilities, and theirs is best for more activity.

I'll see how hard it is to merge in my code over the next few nights. In the meantime I'm fighting thttpd - but that is a post for another day.


Comments are closed on posts which are more than ten days old.

Recent Posts

Recent Tags