Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class DownloadsController < ApplicationController
def show
if @current_user
send_file "#{Rails.root}/downloads/test.pdf"
else
head(:not_found)
end
end
end
# nginx X-Accel-Redirect configuration for Rails and Unicorn
upstream unicorns {
server unix:/srv/APPLICATION/tmp/sockets/unicorn.sock;
# If you're not using unicorn with UNIX domain sockets, you'd do something like this:
# server 127.0.0.01:8080;
}
server {
listen 80;
server_name example.org;
root /srv/APPLICATION/public;
location ~ /downloads/(.*) {
internal;
alias /srv/APPLICATION/downloads/$1;
}
location / {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Sendfile-Type X-Accel-Redirect;
proxy_set_header X-Accel-Mapping /downloads/=/srv/APPLICATION/downloads/;
proxy_pass http://unicorns;
}
}
# Specifies the header that your server uses for sending files
#config.action_dispatch.x_sendfile_header = "X-Sendfile"
# For nginx:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
# If you have no front-end server that supports something like X-Sendfile,
# just comment this out and Rails will serve the files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment