Using Rack::Sendfile #41

ramontayag opened this Issue Nov 4, 2011 · 3 comments


None yet
2 participants

I've been stuck trying to get Rack::Cache to use Rack::Sendfile without any luck. I've sent a message to the mailing list but it seems that there's no activity there.

The details are in this StackOverflow question, but in summary, even with Rack::Sendfile in front of Rack::Cache, my images don't seem to be sent using Rack::Sendfile.

What do I do?

This is how I insert the middleware:

Rails.application.middleware.insert 0, Rack::Sendfile
Rails.application.middleware.insert 1, Rack::Cache, {
  :verbose     => true,
  :metastore   => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/meta"),
  :entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body")
} unless rack_cache_already_inserted

I then opened the Rack gem and played around with sendfile.rb to see if the body being returned by Rack::Cache responds to to_path, but it doesn't.

After more investigating, I found out that Rails 3.1 (which I am on) already inserts Rack::Cache for you in production. This poses a problem though, as the configuration looks like this:

{:metastore=>"rails:/", :entitystore=>"rails:/", :verbose=>true}

I did some puts in the Rack::Cache source, and it seems none of the Entitystores are used. I know for Rack::Sendfile to work, the Disk class must be used as the entitystore.

Now I just need to find a way to use the disk store. I'm don't know where to set these at the moment.

I ended up getting this to work, albeit with nginx & unicorn rather than Apache and Passenger.

You were almost there with your example above, changing the Rack::Cache stores to file:/ rather than rails:/, however you were missing the config.action_dispatch.x_sendfile_header argument to the Rack::Sendfile line. Without that Rack::Sendfile doesn't know which header to send back so doesn't send anything.

This Gist shows my nginx config template and relevant lines in production.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment