我在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/