Page caching with Phusion Passenger

colindensem edited this page Sep 13, 2010 · 2 revisions
Clone this wiki locally

Passenger seems to reset the page_cache_directory upon forking.

Setting config.action_controller.page_cache_directory = RAILS_ROOT + “/public/cache/”
does not work. It will still store the files in the public directory and not in the specified directory.

The page_cache_directory has to be reset after passenger spawns a new process. It is recommended to implement the following in the production.rb environment file:

# config/environments/production.rb
if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
    if forked
      # See
      Rails.cache.instance_variable_get(:@data).reset if Rails.cache.class == ActiveSupport::Cache::MemCacheStore
      ActionController::Base.page_cache_directory = "#{RAILS_ROOT}/public/cache/"

The Memcache statement is intentionally included, but not necessary if you do not use memcache. You may want to leave this out.

In addition to the above, you will want to setup redirects for your site, as mentioned in the deployment guide:

#apache should serve cached pages RewriteRule ^/$ /cache/index.html [QSA] RewriteRule ^([^.]+)$ /cache/$1.html [QSA] #Uncomment for rewrite debugging #RewriteLog /...mysite.../shared/log/rewrite.log #RewriteLogLevel 9

If you are working with nested resources then consider this rewrite rule, for example you get /widgets/1/parts/1.

    RewriteCond %{DOCUMENT_ROOT}/cache/%{REQUEST_FILENAME}.html -s
    RewriteRule ^([^.]+)$ /cache/$1.html [QSA]

Assumes public/cache for cache store. -s is files with size.