Portrait of Edd Dumbill, taken by Giles Turnbull

Subscribe to updates

Feed icon Atom or RSS

or get email updates

What I make

a conference management web application

XTech Conference
a European web technology conference

Load-balancing Mongrel with Apache 2.0

You would be forgiven for thinking that using Mongrel clusters to serve Rails applications was only a good idea if you have Apache 2.2 installed, as this is what most of the instructions on the web advise.

However, there are good reasons for still running Apache 2.0, one of them being that it's the most up to date version packaged in Debian and Ubuntu Linux. So, without mod_proxy_balancer, what can you do?

A simple solution is to use the randomizing feature in mod_apache's rewrite map feature. Say for instance you had 3 Mongrel servers, running on ports 4000 to 4002 on localhost. First create a file map.txt containing these numbers:

ports  4000|4001|4002 

Then ensure the following directives are present in your virtual host configuration:

ProxyRequests Off
ProxyPassReverse / http://localhost:4000/
ProxyPassReverse / http://localhost:4001/
ProxyPassReverse / http://localhost:4002/
ProxyPreserveHost On
RewriteEngine On
RewriteMap servers rnd:/path/to/your/map.txt
RewriteRule ^/(images|stylesheets|javascripts)/?(.*) $0 [L]
RewriteRule ^/(.*)$ http://localhost:${servers:ports}/$1 [P,L]

Of course, Apache config is complex and I advise you to test this properly — it's possible if you are not careful to leave your web server as an open proxy.

Thanks to Matt Biddulph for pointing out this method to me. I've written it up as nobody else seemed to have done so thus far.

blog comments powered by Disqus

You are reading the weblog of Edd Dumbill, writer, programmer, entrepreneur and free software advocate.
Copyright © 2000-2012 Edd Dumbill