Last active
December 21, 2023 07:08
-
-
Save djo/11374407 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
Thanks, this finally helped me!
In my case, Ngnix was logging the error in relation to the rails assets. This is how I solved it, it might help someone else:
Accessing the app on http://localhost (Ngnix) doesn't generate any warning. Accessing http://localhost:3000 (puma) still generates the warning, but I believe this is the expected outcome.