Last Updated: June 04, 2018
·
26.16K
· 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

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

over 1 year ago ·

Thank you!

over 1 year ago ·

Exactly what I was looking for. Thanks!

over 1 year ago ·

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

over 1 year ago ·