2016-04-22 191 views
0

/var/www/storage/文件夹只能找到mp3文件。我想要做的是,使这些文件的每个请求最终下载而不是在浏览器中播放。Nginx强制下载mp3文件

我目前的配置看起来像这样,我无法弄清楚什么是错的。

server { 
     charset utf-8; 
     client_max_body_size 128M; 

     listen 80; ## listen for ipv4 
     server_name dl.domain.com; 
     root  /var/www/storage/; 
     location/{ 
      add_header Content-Disposition: "$request_filename"; 
     } 
    } 

回答

0

浏览器播放或下载的决定取决于content-type标题。如果要强制浏览器下载文件,而不是玩,你的服务器应该为MP3文件返回Content-Type: application/octet-stream

location ~ /mp3folder/.+\.mp3$ { 
types { 
    application/octet-stream; 
} 
} 
0

初始配置几乎是正确的,你都忘了是“附件”:

add_header Content-Disposition "attachment; filename=$request_filename"; 

还建议您按照raven428的建议来执行application/octet-stream。不过,我相信在不调整内容类型的情况下,您将在大多数UA中获得“另存为”。

RFC 2616 sec19.5.1

如果该头在与所述应用程序/ octet-流内容类型的响应中使用的,隐含的建议是,用户代理不应该显示的响应,而是直接进入一`将回复保存为...'对话框。