【问题标题】:Adding custom HTTP headers to nginx X-Accel-Redirect将自定义 HTTP 标头添加到 nginx X-Accel-Redirect
【发布时间】:2014-08-21 19:55:09
【问题描述】:

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

sn-p 用于重定向的轨道下方

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。但我不想那样做。

非常感谢任何帮助。

【问题讨论】:

  • 试试add_header Content-MD5 $upstream_content_md5;
  • @AlexeyTen,我收到了这个错误nginx: [emerg] unknown "upstream_content_md5" variable
  • 糟糕,我是$upstream_http_content_md5。见nginx.org/r/$upstream_http_
  • @AlexeyTen,它有效.. :) 请将此添加为答案,我会接受它

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


【解决方案1】:

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

 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";

【讨论】:

  • 这个答案被低估了。我可以确认没有变量它不起作用。谢谢。
【解决方案2】:

使用add_header Content-MD5 $upstream_http_content_md5;

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

【讨论】:

  • 同样的问题,但与 aws s3 重定向有关。请您看看这里的问题stackoverflow.com/questions/24971724/…。非常感谢任何帮助
  • 如果我在“内部”位置执行 proxy_pass - 那么 $upstream_.. 变量将变为空。如何解决这个问题?
  • @urmurmur 提出新问题
  • nginx will not send returned headers - 似乎Content-Type 是一个例外,即使没有add_header,该标头也会被转发
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2012-04-23
相关资源
最近更新 更多