Mongrel

Mongrel is a lightweight web server that is quite popular with the Ruby on Rails crowd. It’s apparently really great at serving dynamic pages and is often coupled with Apache which serves the static pages. Set up is extremely easy

gem install mongrel

and running it is much the same.

mongrel_rails start -d

There are some things that I found particularly annoying about it. Mostly you have to be in the document root of the rails app when you start the daemon for it to know where everything is.

Because mongrel runs one thread at a time it can only answer one request at a time. This can be problematic when you’ve got a busy site with many requests a second (I’ve heard a single mongrel instance can handle around 25 requests a second, but I couldn’t site my source). To overcome this limitation you can run mongrel behind Apache’s mod_proxy and mod_proxy_balancer which allows you to create some psuedo load balancing. This is all done through Apache’s httpd.conf file (or whatever imports you want to add to it). You’ll need to load the proxy modules, the balancer module and the rewrite module for things to work right. My configuration looks something like this:

<virtualhost *:80>
ServerName myserver.com
DocumentRoot "/path/to/doc/root"

<proxy balancer://mongrel_cluster>        #This is the name of the cluster group, can be anything
BalancerMember http://localhost:xxx0     # the location of your additional mongrel instances
BalancerMember http://localhost:xxx1
</proxy>

ProxyPass /images !                     # Don't let mongrel serve these pages - static content
ProxyPass /stylesheets !
ProxyPass /javascripts !

Alias /appName /full/path/to/your/app       #only needed if app resides somewhere other than docRoot
ProxyPass /appName balancer://mongrel_cluster
ProxyPassReverse /appName balancer://mongrel_cluster/

RewriteEngine On
ProxyRequests Off              #SysAdmins don't usually like open proxies

# Check for static index
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.*)$ $1/index.html [QSA,L]

# Check for Cached pages
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} -d
RewriteRule ^(.*)[^/]$ $1/ [QSA,L]

# Send all other requests to Mongrel to be dynamically generated
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

# Compress text files so they can be loaded more quickly
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml

As you can see its not overly complicated. The rewrite can be intimidating, but they’re recipes mostly you wouldn’t need to change them. There is some good documentation online, but not copious amounts of it. I’d recommend here and here for more resources, but you’re a smart kid, you can find your own answers using google.

Leave a Reply

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a