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

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

问题描述

我正在使用nginx的X-Accel-Redirect在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天全站免登陆