【问题标题】:nginx rails send_file - Duplicate headers received from servernginx rails send_file - 从服务器收到的重复标头
【发布时间】:2012-04-20 19:09:56
【问题描述】:

我已经将我们的 Rails 应用程序设置为通过 send_file 提供文件,并且由于我们不想让我们的应用程序忙于提供文件,因此我们将其通过 X-Accel-Redirect 标头传递给 Nginx。为此,我在我的production.rb 文件中设置了config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect',并像这样设置我的nginx.conf:

# In order to get the site running
# symlink this file to /etc/nginx/sites-enabled/production

upstream unicorn-production {
  server unix:/tmp/unicorn.sock fail_timeout=0;
}

server {
  listen 3000;
  server_name production.localhost;
  root /home/deployer/apps/production/current/public;
  access_log /var/log/nginx/production_access.log;
  rewrite_log on;

  try_files $uri/index.html $uri @unicorn;

  location ~ ^/downloads/(.*)$ {
    internal;
    alias /home/deployer/downloads/$1;
  }

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_redirect off;
    proxy_pass http://unicorn-production;

    proxy_set_header  X-Sendfile-Type   X-Accel-Redirect;
    proxy_set_header  X-Accel-Mapping   /downloads/=/home/deployer/downloads/;

    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

在我的控制器操作中,我执行以下操作: send_file "/home/deployer/downloads/testfile.foo"

.

这在理论上应该是可行的,但是当我访问 mysite.com/mycontroller/download 时,Chrome 告诉我 Duplicate headers received from server: Error 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION): Multiple Content-Disposition headers received. This is disallowed to protect against HTTP response splitting attacks.

非常感谢任何帮助。

【问题讨论】:

    标签: ruby-on-rails google-chrome nginx unicorn


    【解决方案1】:

    可以通过在使用 send_data 时将内容处置文件名括在引号中来修复此错误:

    发件人:

    send_data data, :type => type,
              :disposition=>"attachment; filename=#{filename}"
    

    收件人:

    send_data data, :type => type,
              :disposition=>"attachment; filename='#{filename}'"
    

    我假设在使用 send_file 方法时也是如此

    见:https://github.com/prior/prawnto/pull/16

    【讨论】:

    • 对我不起作用:( - 当我这样做时错误消失:处置:“附件;文件名=\”#{文件名}\“”但下载的文件名是:“20150514_015108_to_015208 .mp4-, attachment” - 似乎 Chrome 出于某种未知原因添加了“-, attachment”:/
    猜你喜欢
    • 2012-11-14
    • 2012-02-24
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 2011-06-05
    • 1970-01-01
    • 2013-11-14
    相关资源
    最近更新 更多