Nginx – enable download of multimedia file instead of streaming

file-sharinghttp2mod-pagespeednginx

I am trying to enable multimedia (Mp3 and Mp4) file downloads on my newly setupped nginx/1.17.6 using HTTP2 server but the file started streaming instead of downloading. Here is my nginx.conf file:

user www-data;
worker_processes  auto;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 4096;

events {
    worker_connections  4096;
    multi_accept on;
    # essential for linux, optmized to serve many clients with each thread
    use epoll;
}


http {
    include       /etc/nginx/mime.types;

    ## Performance
    ## access_log off;
    access_log /var/log/nginx/access.log;
    open_file_cache max=10000 inactive=1200s;
    open_file_cache_valid 1200s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;

    ##Security
    #limit_req_zone $binary_remote_addr zone=one:30m rate=30r/m;  #Allow 30 requests per minute (2 req per sec), check location tag for forcing policy
    #limit_conn_zone $binary_remote_addr zone=two:30m;  #Limit the number of simultaneous connections from a single IP, check location tag for forcing policy
    #limit_conn_log_level warn;
    add_header X-Content-Type-Options nosniff;


    sendfile on;
    sendfile_max_chunk 5m;
    tcp_nopush on;
    keepalive_timeout 90;
    tcp_nodelay on;

    gzip on;
    gzip_comp_level  2;
    gzip_min_length  1000;
    gzip_proxied     expired no-cache no-store private auth;
    gzip_types       text/plain application/x-javascript text/xml text/css application/xml;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    charset utf-8;


    include /etc/nginx/sites-enabled/*;
    include /etc/nginx/conf.d/stub_status.conf;
    add_header Strict-Transport-Security "max-age=15768000; includeSubDomains" always;

    send_timeout 280s;

}

I am using the below code in php to initiate the download:

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("X-Accel-Redirect: /vtemp/" . $fname); //This is nginx specific, eqv. to XSendfile
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$fName\"");
header("Content-Transfer-Encoding: binary");
header('Connection: close');

Pagespeed module is also enabled (could this be an issue?, as this is not enabled on my other working server):

 pagespeed on;
 pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
 pagespeed RewriteLevel OptimizeForBandwidth;

 location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" {
     add_header "" "";
 }

 location ~ "^/pagespeed_static/" { }
 location ~ "^/ngx_pagespeed_beacon$" { }

The same config works on my other server having an olde nginx build and not using HTTP2 protocol, what am I missing here?

Best Answer

Try adding this to your nginx.conf server section:

    location ~* (./?|\mp3|\mp4)$ {
    types { application/octet-stream (.mp3|.mp4); }
    default_type application/octet-stream;
}