3yb8vg
Last Updated: June 04, 2018
·
14.73K
· blat

Force file download with Nginx

For a long time, I've tried to force file download by modifying "Content-type" in headers. Unfortunately it's not a safe solution.

The best way I've found is to send the content as an "attachment" (using "Content-Disposition" header).

This is a sample config to do this with Nginx:

server {
    listen 80;
    server_name my.domain.com;
    location ~ ^.*/(?P<request_basename>[^/]+\.(mp3))$ {
        root /path/to/mp3/
        add_header Content-Disposition 'attachment; filename="$request_basename"';
    }
}

4 Responses
Add your response

13614

Thank you that was exactly what I needed to solve my issue with mp3 download ;)) Very simple and useful.

over 1 year ago ·
14531

Thank you!

over 1 year ago ·
15014

Exactly what I was looking for. Thanks!

over 1 year ago ·
29974

Can any one tell me where to put the above code or how can i use this code ?

4 months ago ·