參考:
http://segmentfault.com/blog/p_chou/1190000000437323?utm_source=tuicool
http://www.netingcn.com/nginx-add-header.html
http://nginx.org/cn/docs/http/ngx_http_rewrite_module.html
項目需要MP3下載的功能,GOOGLE了幾篇文章,參考實現了這個功能,用時大概3個番茄鍾,了解了Nginx內置變量和正則判斷請求URL的方法。
需求:
打開URL:http://localhost/a_short_song.mp3 ,在線播放音樂
打開URL:http://localhost/a_short_song.mp3?download=a.mp3 實現MP3下載,並重命名文件名為指定參數: a.mp3
Nginx配置文件中的配置代碼如下:
location / { index index.html index.php; if ($arg_download){ add_header Content-Disposition "attachment; filename=$arg_download"; } }
問題:中文文件名時有問題,如http://localhost/a_short_song.mp3?download=張三.mp3時,下載的文件名稱為:%E5%BC%A0%E4%B8%89.mp3,有待解決一下。