Last active
January 17, 2023 22:36
-
-
Save un33k/7119264 to your computer and use it in GitHub Desktop.
Enable Nginx to send Content-Dispositions on specific files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
listen *:80; | |
server_name www.example.com; | |
rewrite ^(.*) http://example.com $1 permanent; | |
} | |
server { | |
listen *:80; | |
server_name example.com; | |
# Static directory | |
location /data/ { | |
autoindex on; | |
alias /data/; | |
expires 30d; | |
if ( $request_filename ~ "^.*/(.+\.(zip|tgz|iso|gz))$" ){ | |
set $fname $1; | |
add_header Content-Disposition 'attachment; filename="$fname"'; | |
} | |
} | |
root /srv/www/example.com; | |
access_log /srv/www/example.com/logs/access.log; | |
error_log /srv/www/example.com/logs/errors.log; | |
# Redirect server error pages to the static page /50x.html | |
error_page 500 502 504 /50x.html; | |
location = /50x.html { | |
root /usr/share/nginx/www; | |
} | |
} |
its help me thank u so much
PRs are welcomed!
You dont need to specify the filename. You can just do:
location /attachments/ {
add_header Content-Disposition 'attachment';
proxy_pass http://localhost:8080/;
}
@rsgilbert the filename is to pin down to only specified files of choice. Not all file types!
Afaik you still don't need this complex filename stuff.
location /data/ {
autoindex on;
alias /data/;
if ( $request_filename ~* .*\.(zip|tgz|iso|gz) ){
add_header Content-disposition "attachment";
}
}
doing the same by decreased complexity (and note ~* for case insensitivity). Or am I wrong?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You must put a space after "if" :
if ($request_filename ~ "^.*/(.+.(zip|tgz|iso|gz))$ " ){
set $fname $1;
add_header Content-Disposition 'attachment; filename="$fname"';
}