Here we’ll give an example of manipulation with Content-Disposition based on file type or extension. For the sake of the example, we’ve selected a few extensions that we want to append with Content-Disposition and provide the file name to it. Any file that doesn’t meet this condition will be defined as “noname”.

NginX configuration block

location / {
        set $file_name "noname";
        if ($request_uri ~ \.(pdf|doc?|rtf|txt)$) {                                  
            set $file_name $baseuri;
        }
        add_header Content-Disposition 'attachment; filename="$file_name"';
…
}

CURL to a file from the above list (pdf, doc, docx, rtf, txt)

curl -I cdn.domain.com/example.pdf
HTTP/1.1 200 OK
Date: Sat, 07 Mar 2015 23:20:30 GMT
Content-Type: application/pdf
Content-Length: 2546858
Connection: keep-alive
Cache-Control: max-age=604800
Accept-Ranges: bytes
Server: NetDNA-cache/2.2
Content-Disposition: attachment; filename="example.pdf"
Link: ; rel="canonical"
X-Cache: HIT

CURL to a file not in the above list (html)

curl -I cdn.domain.com/example.html
HTTP/1.1 200 OK
Date: Sat, 07 Mar 2015 23:20:31 GMT
Content-Type: text/html;charset=UTF-8
Content-Length: 115293
Connection: keep-alive
Cache-Control: private
Accept-Ranges: bytes
Server: NetDNA-cache/2.2
Content-Disposition: attachment; filename="noname"
Link: ; rel="canonical"
X-Cache: MISS