Nginx configuration for merb with page caching (file store)

Posted by Guillaume Maury
on Dec 14, 08

This is the nginx configuration I use for this blog. One thing to note is that on the contrary to the rails convention of putting the cache in public, I like to segregate it in the public/page_cache directory.

upstream seshat {
  server 127.0.0.1:4444;
  server 127.0.0.1:4445;
}


# Redirecting all www.gom-jabbar.org trafic to gom-jabbar.org
server {
 listen       80;
 server_name  www.gom-jabbar.org;
 rewrite ^/(.*) http://gom-jabbar.org permanent;

}


# the server directive is nginx's virtual host directive.
 server {
   # port to listen on. Can also be set to an IP:PORT
   listen       80;

   # sets the domain[s] that this vhost server requests for
   server_name  gom-jabbar.org;

   # vhost specific access log
   access_log      /var/log/nginx/seshat.access_log main;
   error_log       /var/log/nginx/seshat.error_log info;


   #Set the max size for file uploads to 1Mb
   client_max_body_size  1M;

   root /sites/seshat_blog/public;
   set $cache_path $document_root/page_cache;
   
   index  index.html;
   
   # this rewrites all the requests to the maintenance.html
   # page if it exists in the doc root. This is for capistrano's
   # disable web task
   if (-f $document_root/system/maintenance.html) {
     rewrite  ^(.*)$  /system/maintenance.html last;
     break;
   }
   
   location / {

     proxy_set_header  X-Real-IP  $remote_addr;
     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header  Host $http_host;
     proxy_redirect    false;

     # The next two if blocks tell nginx to look for the cache
     # serve it if it exists
     if (-f $cache_path$uri.html) {
       rewrite (.*) /cache$1.html break;
     }

     if (-f $cache_path$uri) {
       rewrite (.*) /cache$1 break;
     }
     
     if (-f $cache_path${uri}index.html) {
       rewrite (.*) /cache$1/index.html break;
     }

     if (!-f $request_filename) {
       proxy_pass http://seshat;
       break;
     }
   }
   
   # Add expires header for static content
   # See Steve Souders - High Performance Web Sites rule #3
   # Normally when I add this, I also bundle all my css, javascript
   # with yuicompressor, and add the git commit hash
   # of those files to their name
   # I haven't done it yet with this blog though
   location ~* \.(js|css|jpg|jpeg|gif|png|svg)$ {
     if (-f $request_filename) {
           expires      max;
       break; 
     }  
   }

   error_page  500 502 503 504 /50x.html;
   location = /50x.html {
     root   html;
   }
 }

My merb-cache setup looks like this:

  dependency 'merb-cache', merb_gems_version do 
    Merb::Cache.setup do
      register(:blog_fragment_store, Merb::Cache::FileStore, :dir => Merb.root / 'cache' / 'fragments')
      register(:blog_page_store, Merb::Cache::PageStore[Merb::Cache::FileStore], :dir => Merb.root / 'public' / 'page_cache')
      register(:default, Merb::Cache::AdhocStore[:blog_page_store, :blog_fragment_store])
    end
  end

I’m currently available for hire on a contract basis.

blog comments powered by Disqus