将自定义 HTTP 标头添加到 nginx X-Accel-Redirect [英] Adding custom HTTP headers to nginx X-Accel-Redirect

查看:40
本文介绍了将自定义 HTTP 标头添加到 nginx X-Accel-Redirect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 X-Accel-Redirect 和 nginx 在 rails 中提供受限下载.为了在客户端应用程序中验证我的下载,我尝试将非标准 HTTP 标头 Content-MD5 中的校验和发送到 X-Accel-Redirect 请求.但这不起作用.

I am serving restricted downloads in rails using X-Accel-Redirect with nginx. To validate my downloads in client app, i am trying to send the checksum in the non standard HTTP header Content-MD5 to the X-Accel-Redirect request. But this is not working.

在用于进行重定向的 rails 代码段下方

below the rails snippet used to do the redirection

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 标头.有什么办法可以从 rails 传递我的 Content-MD5 标头吗?

This is not working apparently. I am not able to get the Content-MD5 header in my responses. Is there any way to pass my Content-MD5 header from rails?

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

I know there are ways to do that entirely in nginx, like compiling nginx with perl or lua and easily calculate the MD5 on the fly. But i dont want to do that.

非常感谢任何帮助.

推荐答案

使用 add_header Content-MD5 $upstream_http_content_md5;

由于 X-Accel-Redirect 导致内部重定向,nginx 不会发送返回的标头,但会将它们保留在 $upstream_http_... 变量.所以你可以使用它们.

Since X-Accel-Redirect causes internal redirect nginx will not send returned headers, but it will keep them in $upstream_http_... variables. So you could use them.

这篇关于将自定义 HTTP 标头添加到 nginx X-Accel-Redirect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆