-
-
Save mauriciopasquier/884b9ac2cdf19af3793f6c962bd85076 to your computer and use it in GitHub Desktop.
Rails, Nginx, XSendfile, and X-Accel-Mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Symlink the shared protected folder | |
run "ln -nfs #{shared_path}/protected #{latest_release}/protected" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def download | |
send_file @document.file.path | |
# path: /app/releases/20140101010101/protected/file.pdf | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Defines the internal location to support the X-Sendfile feature | |
# for the delivery of static files from the folder: /app/shared/protected | |
# http://wiki.nginx.org/XSendfile | |
location /protected/ { | |
internal; | |
root /app/shared; | |
} | |
# Rack::Sendfile uses the X-Accel-Mapping header to map the file path (specified in the documents_controller.rb) | |
# to the internal location set in the nginx.conf. | |
# Rack::Sendfile takes a regular expression for the left hand side of the X-Accel-Mapping value: | |
# https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/sendfile.rb#L138 | |
location @unicorn { | |
proxy_set_header X-Accel-Mapping /app/releases/[\d]+/protected/=/protected/; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Specify the header only in the production.rb | |
# in order to allow Rails serving the files in the development: | |
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment