Nginx, download file instead of opening it

June 2, 2016 38 Yehor Rykhnov

Nginx, example of download file instead of opening it

To download the file, you need add a block in nginx configuration of site:

server {
    #...
    location ~ ^/path/to/folder/.*\.mp4$ {
        include /etc/nginx/include/download.conf;
    }
    #...
}

File /etc/nginx/include/download.conf contains the following code:

add_header Content-Disposition "attachment";
add_header Content-Type application/octet-stream;

An example of several extensions to downloa:

server {
    #...
    location ~ ^/path/to/folder/.*\.(mp4|mp3|avi|png)$ {
        include /etc/nginx/include/download.conf;
    }
    #...
}

Then restart nginx:

service nginx reload

or

service nginx restart