我对X-Sendfile、NGinx和Rails的问题越来越不满意了。我已经阅读了几个文档和教程,但我就是不明白这一点。
不管我怎么想,我都得了404分。这是NGINX的X-Sendfile部分。
location / {
proxy_set_header X-Sendfile-Type X-Accel-Redirect;
proxy_set_header X-Accel-Mapping /var/www/cube_storage/uploads/=/cdn; #maps a real path to the internal location
...
} # end of location app
location /cdn {
root /var/www/cube_storage/;
internal;
proxy_set_header X-Sendfile-Type X-Accel-Redirect;
proxy_set_header X-Accel-Mapping /var/www/cube_storage/=/cdn/;
}在my rails应用程序中启用了X-Accel-Redirect:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx以下是负责提供文件的控制器的代码:
def image
@activity = AccountActivity.where(:id => params[:id]).first
if !@activity || !@activity.file_attachment_is_image? || @activity.file_attachment_name != requested_file_name()
l = Logger.new("#{Rails.root}/logs/cdn_activity.log")
l.info(params.inspect)
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found
else
begin
if !params[:version] || params[:version] == "original"
t = @activity.file_attachment # thats the original image!
else
t = @activity.file_attachment.send(params[:version])
end
send_file t.current_path ,:disposition => 'inline'
rescue Exception => ex
l = Logger.new("#{Rails.root}/logs/cdn_activity.log")
l.info("--------------------------")
l.info(Time.now.to_s)
l.info("exception -> #{ex.message}")
l.info("params: #{params.inspect}")
render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found
end
end结束
我使用carrierwave作为存储引擎。也许有人只是看到了我的错误,但我甚至在尝试了几个小时后都不知道。
还有一件事,请求甚至不会出现在development.log中(是的,我也为开发激活了X-Accel )。
向你致敬,亚历克斯
发布于 2013-09-12 04:30:20
明白了:永远不要在nginx的location指令中使用任何rails路由部分。对于这一个,它是有效的:
location / {
proxy_set_header X-Sendfile-Type X-Accel-Redirect;
# Maps a real path to the internal location
proxy_set_header X-Accel-Mapping /var/www/cube_storage/uploads/=/downloads;
} # end of location app
location /downloads {
alias /var/www/cube_storage/uploads/;
internal;
}https://stackoverflow.com/questions/18748366
复制相似问题