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"';
}
}
Written by Mickael
Related protips
4 Responses

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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Nginx
Authors
reiaguilera
279.8K

jamesdullaghan
90.26K
Related Tags
#nginx
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#