Nginx 对某些文件使用Content-Dispositions



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;
  }
}


I believe your $request_filename should actually contain additional parentheses, like "^.*/(.+\.(zip|tgz|iso|gz))$". Otherwise it only matches zip files.

You must put a space after "if" :

if ( " ){
 set $fname $1;
 add_header Content-Disposition 'attachment; filename="$fname"';
 }


You dont need to specify the filename. You can just do:
       location /attachments/ {
        add_header Content-Disposition 'attachment';
        proxy_pass http://localhost:8080/;
    }

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?


https://gist.github.com/un33k/7119264
阅读量: 277
发布于:
修改于: