【问题标题】:How to force file download in the browser, nginx server如何在浏览器、nginx服务器中强制文件下载
【发布时间】:2012-02-21 16:26:53
【问题描述】:

我目前有 2 个图像位置,它们可能是格式(jpg、jpeg、png、gif)

i.domain.com/simage.jpg thumbnail
i.domain.com/image.jpg high quality
i.domain.com/o/image.jpg full resolution

有谁知道如何强制 /o/ 中的图像文件下载而不是在网络浏览器中呈现?

这是我的 conf 文件:http://pastebin.com/dP8kZMzx

#setup subdomain i.domain.com
server {
    server_name i.domain.com;
    access_log /var/log/nginx/i..com.access.log;
    error_log /var/log/nginx/i.domain.com.error.log;

    root /var/www/domain.com/test1/images;
    index index.php index.html index.htm;

    #error_page 403 = /notfound.jpg;
    #error_page 500 = /notfound.jpg;

    location / {            
        #change this to a 404 img file .jpg
        try_files $uri $uri/ /notfound.jpg;

        rewrite  "/s([A-Za-z0-9.]+)?" /small/$1 break;
        rewrite  "/o/([A-Za-z0-9.]+)?" /orig/$1 break;
        rewrite  "/([A-Za-z0-9.]+)?" /medium/$1 break;
    }

}

【问题讨论】:

  • 试图弄清楚如何让它在混合的 Apache/Nginx 设置中工作很长时间。最后只是从 Nginx 直接提供的文件类型列表中排除 pdf 并在 .htaccess 中设置 AddType application/octet-stream .pdf

标签: nginx mime-types


【解决方案1】:

你只需要返回HTTP头Content-disposition:

location ~* /orig/(.+\.jpg)$ {
    add_header Content-disposition "attachment; filename=$1";
}

【讨论】:

【解决方案2】:

我一直在尝试将此功能添加到我自己的 nginx 服务器中。

我在服务器上有一个文件夹,我想为知道路径的任何人打开它。

为此,我在我的 nginx 配置文件中添加了 /file/ 位置。

server {
   listen 80;
   server_name example.com;

   location /file/ {
       alias /var/openStuff/file/;
       add_header Content-disposition "attachment";
   }
}

对 example.com/file/x 的任何请求都将转到 /var/openStuff/file 并查找 x 并强制浏览器下载文件

如果路径是 /file/stuff/things.txt 这也可以工作,并且将从 /var/openStuff/file/stuff/things.txt 提供它

【讨论】:

  • 谢谢你,完美的线路结束了我 3 天的搜索
【解决方案3】:

以下配置对我有用。

server {
    ...
    # Django media
    location /media  {
        alias /var/www/media;  
        types { application/octet-stream .pdf; }
        default_type application/octet-stream;
    }
    ...
}

但 pdf 文件需要以小写 pdf 结尾才能下载。我仍然不知道将 .pdf 和 .PDF 添加到上述配置文件的语法。有什么建议吗? 答案基于来自http://210mike.com/force-file-download-nginx-apache/的信息

【讨论】:

    【解决方案4】:

    稍微混合一下就可以了。强制下载 CRX 文件而不是安装或显示。

    ## Download crx
    location ~* \.crx$ {
        add_header Content-disposition "attachment; filename=$1";
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 1970-01-01
      • 2010-12-27
      • 2018-05-15
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 2015-04-16
      相关资源
      最近更新 更多