8

我正在使用X-Accel-Redirectnginx 在 Rails 中提供受限下载。为了验证我在客户端应用程序中的下载,我尝试将非标准 HTTP 标头中的校验和发送Content-MD5X-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 标头。有什么方法可以从 Rails 传递我的 Content-MD5 标头吗?

我知道有一些方法可以完全在 nginx 中做到这一点,比如用 perl 或 lua 编译 nginx 并轻松计算 MD5。但我不想那样做。

任何帮助深表感谢。

4

2 回答 2

19

采用add_header Content-MD5 $upstream_http_content_md5;

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

于 2014-07-01T11:37:49.297 回答
3

我已经尝试过接受的答案,但它对我不起作用。但这有效:

 set $authorization "$upstream_http_authorization";
 proxy_set_header Authorization $authorization; # Pass on secret from back end

(从本文复制粘贴https://clubhouse.io/developer-how-to/how-to-use-internal-redirects-in-nginx/

有趣的是,提取变量很重要。这对我不起作用:

 proxy_set_header Authorization "$upstream_http_authorization";
于 2021-06-07T11:38:09.233 回答