Nginx覆盖Rails 4 X-Accel-Redirect URL上的内容类型

时间:2014-01-16 10:42:12

标签: ruby-on-rails ruby-on-rails-3 nginx ruby-on-rails-4

我通过X-Accel-Redirect通过nginx在rails中发送一些受保护的文件。我已成功运行此设置过去1年(使用rails 3),一切都很好。

我的nginx.conf是

     proxy_set_header X-Accel-Mapping /var/www/app/current/public/restricted=/download_restricted/;
     passenger_set_cgi_param HTTP_X_ACCEL_MAPPING /var/www/app/current/restricted=/download_restricted/;
     passenger_pass_header X-Accel-Redirect;

     location /download_restricted {
        internal;
        proxy_cache editor;
        expires max;
        #add_header  Cache-Control public;
        add_header Cache-Control "public, max-age=315360000";
        alias /var/www/app/current/public/restricted;
     }

我的rails代码是

headers['X-Accel-Redirect'] = '/download_restricted/uploads/assets/'+ params[:asset_id]  + '/res/' + params[:res] + '.' + params[:format]
        headers['X-Accel-Expires'] = 'max'
        headers['Content-type'] = MIME::Types.type_for(params[:format])
        headers['disposition'] = 'inline'
        request.session_options[:skip] = true
        render :nothing => true 

此设置在rails 3项目中仍能正常运行。但轨道4& nginx,内容类型始终为text/html

我正在路径中提供音频/视频/图像文件。由于内容类型出错,浏览器无法正确呈现资产。

在这些请求的set/overwrite the content type中是否存在nginx的方式?

1 个答案:

答案 0 :(得分:0)

经过一些实验,结果证明我必须将content_type传递给render :nothing才能使x-accel-redirect识别内容类型并且它不关心headers['Content-type']

现在这段代码工作正常

render :nothing => true, :content_type => MIME::Types.type_for(params[:format]).first.content_type