Force file download with Nginx

24 Nov 2012

For a long time, I’v 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"';
    }
}