Nginx:当Rails返回X-Accel-Redirect标头时,是否跳过了post_action?

时间:2017-07-09 07:31:10

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

我的Nginx 1.4.2实例的配置中的post_action未触发(或似乎不是)。我想知道是不是因为Rails返回X-Accel-Redirect标题。最终目标是跟踪S3上托管的下载是否已完成。

第1步:请求在http://host/download/点击nginx ...

location ~ /download/ {
  proxy_pass http://rails-app-upstream;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  post_action @finished;
}

步骤2:在对数据库中的下载文件请求进行身份验证并标记下载开始后,Rails会返回带有以下标题的请求:

{ 
   'X-Accel-Redirect' => '/s3-zip/my-bucket.s3.amazonaws.com/downloads/0001.jpg`
   'Content-Disposition' => "attachment; filename=download.jpg",
   'X-Download-Log-Id' => log.id.to_s,
   'X-Download-Mem-Id' => log.membership_id.to_s
}

第3步: Nginx捕获x-accel-redirect标头并点击定义的位置:

location ~ "^/s3-zip/(?<s3_bucket>.[a-z0-9][a-z0-9-.]*.s3.amazonaws.com)/(?<path>.*)$" {
  # examples:
  #   s3_bucket = my-bucket.s3.amazonaws.com
  #   path = downloads/0001.mpg
  #   args = X-Amz-Credentials=...&X-Amz-Date=...
  internal;

  access_log /var/log/nginx/s3_assets-access.log main;
  error_log /var/log/nginx/s3_assets-error.log warn;

  resolver 8.8.8.8 valid=30s; # Google DNS
  resolver_timeout 10s;

  proxy_http_version 1.1;
  proxy_set_header Host $s3_bucket;
  proxy_set_header Authorization '';

  # remove amazon headers
  proxy_hide_header x-amz-id-2;
  proxy_hide_header x-amz-request-id;
  proxy_hide_header Set-Cookie;
  proxy_ignore_headers "Set-Cookie";

  # no file buffering
  proxy_buffering off;

  # bubble errors up
  proxy_intercept_errors on;

  proxy_pass https://$s3_bucket/$path?$args;
}

错过了步骤:步骤1中post_action调用的以下位置永远不会被触发。这是因为x-accel-redirect标题,还是因为第3步使用proxy_pass或其他内容?一旦nginx完成将下载标记为已完成的请求,最后一个位置将再次调用rails路由。

location @finished {
  internal;

  rewrite ^ /download/finish/$sent_http_x_download_log_id?bytes=$body_bytes_sent;
}

0 个答案:

没有答案