Posted by rp8 on Thursday, September 25, 2008

RAILS is perfect for serving dynamic contents but it’s just wasteful for static contents.

In a typical RAILS deployment where Apache is used as front-end and Mongrel cluster as back-end, we can leverage mod-rewrite to avoid sending the requests for static contents to Mongrel.

Here’s an example site:

1. RAILS is deployed in /home/site and DocumentRoot = /home/site/public

2. The static contents are in /home/site/static and don’t forgot to change file mode for SSI.

chmod +x abc.html

Here’s the site conf file (site.conf for Apache2):

<VirtualHost *>
  DocuemntRoot /home/site/public
  ServerName www.site.com
  ServerAlias site.com
  XBitHack on

Alias /static /home/site/static <Directory /home/site/static> AllowOverride Options Options +Includes +ExecCGI -Indexes Order allow,deny Allow from all <Directory /home/site/public> Options +ExecCGI -Indexes Order allow,deny Allow from all RewriteEngine on
  1. static contents
    RewriteRule ^/static.* $0 [QSA,PT,L]
  1. static index.html for /
    RewriteRule ^/$ /index.html [QSA]
  1. cached RAILS pages
    RewriteRule ([.]+)$ $1.html [QSA]
  1. proxy to mongrel cluster
    RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
    RewriteRule ^/(.*) balancer://site_cluster%{REQUEST_URI} [P,QSA,L]

<Proxy balancer://site_cluster>
BalancerMember http://127.0.0.1:5001
BalancerMember http://127.0.0.1:5002