Apache 2.2 + Mongrel Cluster

I tested several setups, based on Apache 2.0.x, Lighttpd, FastCGI, and SCGI. None of them was easy to setup and none of them performed anywhere near efficient. Maybe I was doing some things wrong. Maybe Windows is not the optimal platform. Either way, I don’t care. I needed something both easy to setup and maintain, and to run efficiently on Windows.

Having read this post by Johnathan Weiss a while back, I wanted to try the setup he describes. Except that in his setup, all requests get proxied to and serviced by Mongrel, even static data and images. This will leave Apache idle while placing unnecessary load on the Mongrel cluster, besides missing all Apache goodies like compression!

Done some more searching and found that Coda Hale has the most elegant solution to the problem. I simplified his solution to the steps below. But before trying anything, you should read Coda’s post to understand what’s going on.

Here are the configuration changes to httpd.conf:

  1. Enable the following mods by uncommenting the lines (delete the # prefix):
    #LoadModule proxy_module modules/mod_proxy.so
    #LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
    #LoadModule proxy_http_module modules/mod_proxy_http.so
    #LoadModule rewrite_module modules/mod_rewrite.so
  2. Add an alias for your Rails application:
    Alias /rails/cookbook "D:/rails_apps/cookbook/public/"
  3. Enable access to your applications folder:
    <Directory "D:/rails_apps">
        AllowOverride All
        Options FollowSymLinks
        Order allow,deny
        Allow from all
    </Directory>
  4. Create a balancer proxy for the Mongrel cluster:
    <Proxy balancer://mongrelcluster>
        BalancerMember http://localhost:3000
        BalancerMember http://localhost:3001
    </Proxy>
  5. [Optional to enable compression] Enable the following module:
    #LoadModule deflate_module modules/mod_deflate.so

    and insert the following lines:

    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch bMSIE !no-gzip !gzip-only-text/html
  6. [Optional to enable /balance-manager and/or /server-status] Enable the following module:
    #LoadModule status_module modules/mod_status.so

    and enable access to their locations:

    <Location /balancer-manager>
        SetHandler balancer-manager
        Order Deny,Allow
        Deny from all
        Allow from localhost
    </Location>
    
    <Location /server-status>
        SetHandler server-status
        Order Deny,Allow
        Deny from all
        Allow from localhost
    </Location>

And to D:\rails_apps\cookbook\public\.htaccess, make the following changes:

  1. Comment out the following lines (prefix with #):
    AddHandler fastcgi-script .fcgi
    AddHandler cgi-script .cgi
    Options +FollowSymLinks +ExecCGI
  2. Add a rewrite line to match the previous alias line:
    RewriteBase /rails/cookbook
  3. Comment out the last rewrite line:
    RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

    and replace it with the following rewrite line:

    RewriteRule ^(.*)$ balancer://mongrelcluster%{REQUEST_URI} [P,QSA,L]

Finally, a couple of optional changes to Rails to map to your default controller:

  1. In D:\rails_apps\cookbook\config\routes.rb add the line:
    map.connect '', :controller => 'recipe'
  2. Rename or delete D:\rails_apps\cookbook\public\index.html.

Note: Mongrel is not part of the default Rails installation. If you haven’t done so, install Mongrel by running the following on a command prompt:

gem install mongrel --include-dependencies

Finally, spawn the Mongrel servers by running the following commands on a command prompt in your Rails application folder (note that the prefix parameter should match the previous alias and rewrite lines):

mongrel_rails start -e production -p 3000 --prefix /rails/cookbook
mongrel_rails start -e production -p 3001 --prefix /rails/cookbook

Et voilĂ ! Restart Apache and browse to http://localhost/rails/cookbook.

Posted in web applications.

One Response to “Apache 2.2 + Mongrel Cluster”

  1. Vitamin Features » 5 Ways to Optimize AJAX in Ruby on Rails Says:

    [...] Some links on gzip compress: mod_deflate (apache 2.2), thread on config’ing lighty with gzip, apache 2.2 + mod_deflate tutorial. [...]

Leave a Reply