ruby-on-rails - 将自定义HTTP header 添加到Nginx X-Accel-Redirect

标签 ruby-on-rails nginx custom-headers x-accel-redirect

我在Nginx中使用X-Accel-Redirect在Rails中提供受限下载。为了验证我在客户端应用中的下载,我正在尝试将非标准HTTP header Content-MD5中的校验和发送到X-Accel-Redirect请求。但这是行不通的。

在用于重定向的rails片段下方

headers['X-Accel-Redirect'] = '/download_public/uploads/stories/' + params[:story_id] +'/' + params[:story_id] + '.zip'
            headers['X-Accel-Expires'] = 'max'
            checksum = Digest::MD5.file(Rails.root.dirname.to_s+'/public/uploads/stories/' + params[:story_id] +'/' + params[:story_id] + '.zip').hexdigest
            headers['Content-MD5'] = checksum
            request.session_options[:skip] = true
            render :nothing => true, :content_type => MIME::Types.type_for('.zip').first.content_type

这是nginx部分
location /download_public { 
 internal;
 proxy_pass_header Content-MD5;
 add_header Cache-Control "public, max-age=315360000";
 add_header Content-Disposition "inline"; 
 alias /var/www/sss/public; 
}

这显然不起作用。我无法在响应中获取Content-MD5 header 。有什么方法可以从Rails传递Content-MD5 header 吗?

我知道有很多方法可以完全在nginx中做到这一点,例如使用perl或lua编译nginx并轻松地动态计算MD5。但是我不想那样做。

任何帮助深表感谢。

最佳答案

使用add_header Content-MD5 $upstream_http_content_md5;
由于X-Accel-Redirect导致内部重定向,nginx不会发送返回的 header ,但会将它们保留在 $upstream_http_... 变量中。因此,您可以使用它们。

关于ruby-on-rails - 将自定义HTTP header 添加到Nginx X-Accel-Redirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24507157/

相关文章:

c - C 中 .h 文件的问题

javascript - EventSource 连接中的自定义 header

ruby-on-rails - 为什么 Ruby on Rails 7.x.x 不呈现公共(public)文件夹?不是网站图标或 404.html 页面。我使用我的模块,但它不起作用

html - XMLHttpRequest 无法加载 http ://localhost:9090/receive. 请求的资源上不存在 'Access-Control-Allow-Origin' header

linux - 中国的WordPress/网站托管

Nginx 安装在 Mac OS X Lion 上。找不到 PCRE

ios - 在 UITableView 中设置自定义标题 View

ruby-on-rails - 设计注册 Controller - 拆分全名并填充名字和姓氏属性

ruby-on-rails - 如何配置 spork 以创建 2 个以上的进程

ruby-on-rails - 我无法在 Ruby on Rails 上提供静态图像