Rails and Nginx: caching

Rails using Phusion Passenger is actually pretty quick after Passenger has first loaded.

And adding caching makes it pretty zippy.

Adding caching via Nginx makes it super fast. Cached pages bypass Rails and are served directly by Nginx which is incredibly fast.

Here’s a guide to using Rails with Nginx. Assuming you’ve got both installed, you need to:

1. add a flag to enable, e.g.:

config.action_controller.perform_caching = true

2. add caches_page

E.g. 

class ProductsController < ActionController

 

  caches_page :index

 

  def index

    @products = Products.all

  end

end

Rails guide on page caching:

http://guides.rubyonrails.org/caching_with_rails.html#page-caching

Nginx page caching:

http://www.modrails.com/documentation/Users%20guide%20Nginx.html#_page_caching_support

Note that this assumes your cache will be in the public/ folder. For a non-standard page cache directory you’ll need to use mod_rewrite.

Leave a Reply

Your email address will not be published. Required fields are marked *