3yb8vg
Last Updated: February 25, 2016
·
11.63K
· blat
2f1a6b664834c9197cd9e602598d996a

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"';
    }
}
Say Thanks
Respond

3 Responses
Add your response

13614
E6c4bdab1b1754154a9b509defc19426

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

over 1 year ago ·
14531
Screen shot 2012 04 24 at 1.16.07 pm normal

Thank you!

over 1 year ago ·
15014
B6a935e0142d2593f67e04c1708d0427

Exactly what I was looking for. Thanks!

over 1 year ago ·
Awesome Job

0957a9c2 0855 11e8 909a 82469fe19f14
Senior Backend Engineer
·
San Francisco, California
·
Full Time