0

I have some mp3 files in different directories other than root directory.

When users click on the links to mp3, instead of downloading it starts streaming in most browsers (mostly in mobile browsers).

I have tried adding the below code in the .htaccess, but no luck

<FilesMatch "\.(?i:mp3)$">
  ForceType application/octet-stream
 Header set Content-Disposition attachment
</FilesMatch>

My server is build in apache and nginxcp (nginx admin). Can anyone help me out?

EDIT: Below is the edited file for my domain vhost file. But after i rebuild host and restart nginx, the file change to its original one and the code "location ~ .mp3$ { add_header Content-Disposition attachment; }" becomes disappears.

server {
          error_log /var/log/nginx/vhost-error_log warn;
          listen 198.xxx.xx.xxx:80;
          listen [::]:80;
      server_name dl.xxxx.com www.dl.xxxx.com;
          access_log /usr/local/apache/domlogs/dl.xxxx.com-bytes_log bytes_log;
          access_log /usr/local/apache/domlogs/dl.xxxx.com combined;
          root /home/xxxx/public_html/download;
          #location / {
          location ~*.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
          expires 1M;
          try_files $uri @backend;
          }
          location / {
      error_page 405 = @backend;
          add_header X-Cache "HIT from Backend";
          proxy_pass 198.xxx.xx.xxx:8081;
          include proxy.inc;
      include microcache.inc;
          }
          location @backend {
          internal;
          proxy_pass http://198.xxx.xx.xxx:8081;
          include proxy.inc;
      include microcache.inc;
          }
          location ~ .*\.(php|jsp|cgi|pl|py)?$ {
          proxy_pass http://198.xxx.xx.xxx:8081;
          include proxy.inc;
      include microcache.inc;
          }
          location ~ /\.ht {
          deny all;
          }
          location ~ \.mp3$ { add_header Content-Disposition attachment; }
        }
Heider Kumar
  • 135
  • 1
  • 1
  • 7
  • Have you checked whether your `Content-Disposition` header is being added (in your browser’s Network panel)? – Ry- Aug 06 '16 at 07:44
  • Sorry!! Headers?? I rebuild apache 3 with headers. – Heider Kumar Aug 06 '16 at 07:44
  • In the nginx admin config, include mime.types and default_type application/octet-stream are already there. Then also not working. – Heider Kumar Aug 06 '16 at 07:50
  • Do you have curl installed? What shows up if you `curl -I http://example.com/file.mp3` (replace with the actual URL for your MP3)? – Ry- Aug 06 '16 at 07:51
  • yes curl is there... – Heider Kumar Aug 06 '16 at 07:52
  • HTTP/1.1 200 OK Server: nginx admin Date: Sat, 06 Aug 2016 07:54:11 GMT Content-Type: audio/mpeg Content-Length: 6122213 Last-Modified: Fri, 29 Jul 2016 07:15:31 GMT Connection: keep-alive ETag: "579b0293-5d6ae5" Expires: Mon, 05 Sep 2016 07:54:11 GMT Cache-Control: max-age=2592000 Accept-Ranges: bytes – Heider Kumar Aug 06 '16 at 07:54
  • Okay. Remove your Apache configuration, then, as Nginx appears to be handling everything, and add a `location ~ \.mp3$ { add_header Content-Disposition attachment; }` block to Nginx. – Ry- Aug 06 '16 at 08:02
  • where i have to put the location code? Is it in the nginx config file? Can you please show me from the above nginx admin config file? – Heider Kumar Aug 06 '16 at 08:06
  • Put it at the end of the appropriate `/etc/nginx/vhosts/yoursite.com.conf`. – Ry- Aug 06 '16 at 08:09
  • No it is not working. – Heider Kumar Aug 06 '16 at 08:41
  • @Ryan I insert the code in my domain.com conf file. Now after i rebuild the vhost and restart the nginx, i have seen that the above code disappears from the file. – Heider Kumar Aug 06 '16 at 12:19
  • [Apache shows the contents of MP4 file rather than a download prompt](https://stackoverflow.com/q/13341453/608639), [Downloading instead of showing image or video file](https://stackoverflow.com/q/27955890/608639), [How can I download a MP4 file instead of playing it on browser?](https://stackoverflow.com/q/29800166/608639), etc. – jww Oct 18 '18 at 07:49

0 Answers0