我已经设置了我们的Rails应用程序来通过send_file
提供文件,因为我们不想让我们的应用程序忙于为文件提供服务,所以我们使用X-Accel-Redirect
标头将其交给Nginx。为此,我在config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
文件中设置了production.rb
,并设置了我的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.
非常感谢任何帮助。
答案 0 :(得分:2)
使用send_data:
时,可以通过将内容处理文件名包装在引号中来修复此错误自:
send_data data, :type => type,
:disposition=>"attachment; filename=#{filename}"
要:
send_data data, :type => type,
:disposition=>"attachment; filename='#{filename}'"
我认为使用send_file方法时同样适用