Created
January 17, 2013 16:44
-
-
Save ozgun/4557407 to your computer and use it in GitHub Desktop.
Rails 3.2.x: Serving files with nginx using X-Accel-Redirect header (capistrano deploy) (2. method)
This file contains hidden or 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
| # Assumptions: | |
| * Rails 3.2.x | |
| * nginx | |
| * capistrano deploy | |
| * "X-Accel-Mapping header missing" messages in nginx error.log file | |
| * You want to serve static files with nginx instead of Rails | |
| # nginx configuration: | |
| # This is required but the path and url is not important | |
| passenger_set_cgi_param HTTP_X_ACCEL_MAPPING "/home/user/rails_app/shared/xyz/=/abc/"; | |
| location ~ ^/home/user/rails_app/releases/\d+/files/(\d\d\d)/(\d\d\d)/(\d\d\d)/(.*)$ { | |
| alias /home/user/rails_app/shared/files/$1/$2/$3/$4; | |
| internal; | |
| } | |
| # DocumentsController: | |
| def index | |
| @document = Document.find(params[:id]) | |
| # @document.path #=> /home/user/releases/20130111141529/files/000/000/001/file.txt | |
| send_file @document.path | |
| end | |
| # Rails: config/environments/production.rb | |
| config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' | |
| # Request: | |
| http://localhost:3000/documents/1 | |
| # References: | |
| * http://stackoverflow.com/questions/954470/serving-large-files-through-nginx-via-rails-2-3-using-x-sendfile | |
| * http://airbladesoftware.com/notes/rails-nginx-x-accel-mapping | |
| * http://wiki.nginx.org/HttpCoreModule#alias | |
| * http://greenlegos.wordpress.com/2011/09/12/sending-files-with-nginx-x-accel-redirect/ | |
| * http://stackoverflow.com/questions/6237016/message-x-accel-mapping-header-missing-in-nginx-error-log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment