2013-10-12 82 views
10

我有一个下载链接,转到使用send_file的控制器中的一个方法,以便我可以重命名该文件(它是一个带有uuid作为文件名的MP3)。点击链接后,我会在NGINX日志和Rails日志中看到请求,但下载之前最多需要90秒。我已经尝试了各种设置与proxy_buffers和客户端_ * _缓冲区没有任何影响。我有一个HTML5音频播放器,它使用文件的真实URL并立即传输文件。NGINX下载缓慢,以send_file开头

我NGINX配置:

upstream app { 
    server unix:/home/archives/app/tmp/unicorn.sock fail_timeout=0; 
} 

server { 
    listen  80 default deferred; 
    server_name archives.example.com; 
    root  /home/archives/app/public/; 

    client_max_body_size 200M; 
    client_body_buffer_size 100M; 
    proxy_buffers 2 100M; 
    proxy_buffer_size 100M; 
    proxy_busy_buffers_size 100M; 

    try_files /maintenance.html $uri/index.html $uri.html $uri @production; 

    location @production { 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_set_header X-Sendfile-Type X-Accel-Redirect; 
    proxy_set_header X-Accel-Mapping /home/archives/app/public/uploads/audio/=/uploads/audio/; 
    proxy_redirect off; 
    proxy_pass http://app; 
    } 

    location ~ "^/assets/*" { 
    gzip_static on; 
    expires  max; 
    add_header Cache-Control public; 
    } 

    location ~ (?:/\..*|~)$ { 
    access_log off; 
    log_not_found off; 
    deny all; 
    } 

    error_page 500 502 503 504 /500.html; 
    location = /500.html { 
    root /home/archives/app/public; 
    } 
} 

Rails的控制器:

def download 
    send_file @audio.path, type: @audio_content_type, filename: "#{@audio.title} - #{@audio.speaker.name}" 
end 
+0

你有'config.action_dispatch.x_sendfile_header ='X-Accel-Redirect'#for NGINX'在你的环境配置中注释掉了吗? – hamitron

+0

如果你在公共文件夹中移动你的mp3,你可以直接通过浏览器访问它,只是为了测试性能(绕过控制器逻辑)。如果它的速度仍然很慢,那么可能是NGINX配置问题。也尝试用webbrick(rails s -b 0.0.0.0)运行你的应用程序,看看是否有什么区别? – Roger

回答

0

经过测试,我发现它是turbolinks造成的问题。它在后台执行XHR请求,首先下载文件,然后允许浏览器实际下载文件。在我的链接中添加'data-no-turbolink'='true'后,立即下载文件。

0

也许是缓慢的,因为你已经设置过大的代理缓存? 100M代理缓冲区意味着您的服务器将在开始将数据发送到目的地之前从原始数据下载100M。默认值是32kB,512kB就是一个不错的数字。