So that /media/file.jpg
opens in browser and /media/file.jpg?dl=1
forces browser to download.
Here's my current config, that doesn't work:
location /media/ {
autoindex off;
root /home/my_app/media/;
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 365d;
add_header Pragma public;
add_header Cache-Control "public";
}
# Allow forcing download by query param `dl`
if ($arg_dl = "1") { # why this doesn't work?
add_header Content-disposition "attachment; filename=$1";
}
}
The
if
statement is never reached, because only thelocation
is matched when a file is requested.If the URL
/media/
is in/home/my_app/media/
directory, you cannot useroot
like that. You need to either usealias /home/my_app/media/
orroot /home/my_app/
.You should try this (updated version):