, , , ,

The Rails 2 way of using x_sendfile: When using send_file, pass an options hash with options[:x_sendfile] = true. This will take care of streaming the file via Apache freeing up Rails.

Changes in Rails 3:

The x_sendfile option to send_file is now deprecated.

There is a new application configuration “config.action_dispatch.x_sendfile_header” set to “X-Sendfile” defined  in config/production.rb. This means that send_file automatically gets the X-Sendfile headers in the production environment. For other environments, we need to copy this setting manually.

The main backend change done in Rails 3 for send_file is to use the Rack::Sendfile middleware. As documented in the link, a request header need to be set before starting to process the request – “RequestHeader Set X-Sendfile-Type X-Sendfile”.

The log no longer prints “Sent X-Sendfile header..” when send_file is used with X-Sendfile option like it used to be in Rails 2. The message is changed to simply log “Sent file..”.

Note: mod_xsendfile needs to be properly enabled in Apache as a prerequisite. Nginx also has its setting defined in production.rb, but this post dealt only with Apache.