How to set content disposition header for nginx server to force content download


Nginx (pronounced as engine x) is an HTTP server, reverse proxy server, as well as a mail proxy server

Nginx is well known for its high performance, stability, efficiency, rich feature set, simple configuration, and low resource consumption.

content disposition headers direct web browsers to save content instead of attempting to render it

To set content disposition open your desired server configuration in "/etc/nginx/sites-available/"

within your configuration add code below within your server {block}


location ~* (mp3|ogg|wav)$ {
    add_header Content-Disposition "attachment";

}

mp3, ogg and wav are example file extensions matched by regular expressions rules

Test configuration by acessing mp3, ogg and wav files from your webserver

Alternatively you can force custom filenames as shown below "$1" substitutes filenames sent to clients. As an example its value corresponds to the requested file's extension


location ~* (mp3|ogg|wav)$ {

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

}

Comments