如何在浏览器,nginx服务器中强制下载文件

eee*_*eek 11 nginx mime-types

我目前有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
Run Code Online (Sandbox Code Playgroud)

有谁知道如何强制/ o /下载图像文件而不是在Web浏览器中渲染?

这是我的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;
    }

}
Run Code Online (Sandbox Code Playgroud)

Ale*_*rov 23

您只需要返回HTTP标头Content-disposition:

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


Tho*_*hor 8

我一直在尝试将此功能放入我自己的nginx服务器中.

我在服务器上有一个文件夹,我想对知道路径的人开放.

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

server {
   listen 80;
   server_name example.com;

   location /file/ {
       alias /var/openStuff/file/;
       add_header Content-disposition "attachment";
   }
}
Run Code Online (Sandbox Code Playgroud)

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

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


lea*_*yUI 5

下面的配置对我有用。

server {
    ...
    # Django media
    location /media  {
        alias /var/www/media;  
        types { application/octet-stream .pdf; }
        default_type application/octet-stream;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

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