Skip to content

IceskYsl/Nginx-X-Accel-Redirect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Nginx-X-Accel-Redirect

This Rails plugin allows users to take advantage of nginx’s X-Accel-Redirect header, similar to X-Send-File and Content-Range.

Credits

This plugin was inspired in the x_send_file plugin, which was created before the option :x_sendfile dropped in Rails send_file method (supported since version 2.1).

Install

You should install it as a plugin, running this command on your projects root directory:

$ script/plugin install git://github.com/IceskYsl/Nginx-X-Accel-Redirect.git

If everything goes correctly, your application is now ready to use the nginx’s X-Accel-Redirect transparently.

Usage

X-Accel-Redirect has an equivalent method to Rails send_file, called x_accel_redirect. Using is as easy as:

x_accel_redirect "/path/to/file", :filename => "filename",:root=>"/path/to/root"

# Options:
# * <tt>:filename</tt> - suggests a filename for the browser to use.
#   Defaults to File.basename(path).
# * <tt>:type</tt> - specifies an HTTP content type.
#   Defaults to 'application/octet-stream'.
# * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.
#   Valid values are 'inline' and 'attachment' (default).
# * <tt>:status</tt> - specifies the status code to send with the response. Defaults to '200 OK'.
# * <tt>:url_based_filename</tt> - set to true if you want the browser guess the filename from
#   the URL, which is necessary for i18n filenames on certain browsers
#   (setting :filename overrides this option).
# * <tt>:header</tt> - specifies the name to use for the X-Sendfile HTTP header
#   Defaults to 'X-Sendfile'.
# * <tt>:file_paths</tt> - list of acceptible file paths
# * <tt>:root</tt> - the root directory is removed when file path specified in header
#
# Simple download:
#   x_accel_redirect '/path/to/file'
#
# Show a JPEG in the browser:
#   x_accel_redirect('/path/to/image.jpg', :type => 'image/jpeg', :disposition => 'inline')
#
# x_accel_redirect's options and defaults mirror those of send_file.  Please see
# ActionController::Streaming#send_file for more detailed information about
# the options, HTTP specs, and possible security issues.

All send_file options are available using this method. Now you can even let your old :x_sendfile option since the plugin will discard it.

Overriding Rails default send_file

You can override Rails default method called send_file with x_accel_redirect.

Just add the following line to your environment.rb:

XAccelRedirect::Plugin.replace_send_file!

And it’s done. Now everytime you call send_file you’ll be using x_accel_redirect instead.

You can still access the normal send_file even after specifying the on environment.rb, using the send_file_without_x_accel_redirect method.

Notes

For further references, please refer to wiki.nginx.org/NginxXSendfile

About

Nginx-X-Accel-Redirect is a plugin for rails to send file via nginx.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages