Created
December 30, 2010 12:24
-
-
Save yas375/759729 to your computer and use it in GitHub Desktop.
Контролируемое скачивание файлов на rails 2 с nginx
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
Контролируемое скачивание файлов через rails 2 в связке с nginx. | |
Используется заголовок X-Accel-Redirect nginx'а, который делает так, | |
что файл отдаёт не приложение, а сам сервер, таким образом на приложение | |
меньше нагрузки. | |
Ссылки по теме: | |
http://groups.google.com/group/ror2ru/browse_thread/thread/1733088704f9278e - | |
обсуждение в группе ror2ru | |
http://wiki.nginx.org/XSendfile |
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
class Attach < ActiveRecord::Base | |
has_attached_file(:file, | |
:url => '/system/files/:id/:basename.:extension', | |
:path => "#{RAILS_ROOT}/files/:class/:id/:basename.:extension") | |
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
class DownloadsController < ApplicationController | |
def show | |
attach = Attach.find(params[:id].to_i) | |
# checking permissions and incrementing download counter goes here | |
head(:x_accel_redirect => attach.file.path.sub(RAILS_ROOT, ''), | |
:content_type => attach.file.content_type) | |
end | |
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
http { | |
passenger_root /home/yas/.rvm/gems/ree-1.8.7-2010.01/gems/passenger-3.0.0; | |
passenger_ruby /home/yas/.rvm/wrappers/ree-1.8.7-2010.01/ruby; | |
include mime.types; | |
default_type application/octet-stream; | |
server { | |
listen 80; | |
server_name zachet.yas; | |
rewrite ^/system/files/(.*)/(.*) /downloads?id=$1&path=$2 last; | |
root /home/yas/ruby/helper/public; # <--- be sure to point to 'pu2blic'! | |
rails_env development; | |
passenger_enabled on; | |
location /files { | |
root /home/yas/ruby/helper; | |
internal; | |
} | |
} | |
sendfile on; | |
} |
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
ActionController::Routing::Routes.draw do |map| | |
map.resource :downloads, :only => :show | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment