The complete nginx solution to sending flowers and files with rails

Rails send_file is effed. It may send you flowers and whisper sweet nothingsblythedoll into your ear, but believe me, it will steal your memory, your lover and your freetime. If you are using apache or lighttpd, all the cool kids are using the x_send_file plugin. Nginx fans, check out my monkey patch so you too can send files, flowers and love without crashing mongrels with nginx’s equivalent X-Accel-Redirect header.

1. Install the x_send_file plugin.

ruby script/plugin install http://john.guen.in/svn/plugins/x_send_file

2. Copy monkey patch to

Replace vendor/plugins/x_send_file/lib/x_send_file/controller.rb with http://www.acidlunchbox.com/blythe/rails/x_send_file/controller.rb

3. Configure things

nginx.conf

remember to also add to server section if sending over ssl

location /files {

root /data/spongecell;

internal;

}

environment.rb

#send files through nginx

XSendFile::Plugin.options.update(

:header => ‘X-Accel-Redirect’,

:root => ‘/data/spongecell’,

:file_paths => ‘/data/spongecell/files’

)

In this example, x_send_file will create the following response header

>> x_send_file ‘/data/spongecell/files/snow/iheartbigdumps.jpg’

>> response.headers['X-Accel-Redirect']

=> ‘/files/snow/iheartbigdumps.jpg’

 

Other resources:

Nginx X-Accel-Redirect

Nginx X-Accel-Redirect with rails and php examples

 

 

 

Leave a Reply