Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force file download in the browser, nginx server

I currently have 2 image locations and they may the formats (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

Does anyone know how I can force image files in /o/ to download rather than render in the web browser?

Here's my conf file: 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;
    }

}
like image 307
eeek Avatar asked Jan 29 '12 15:01

eeek


People also ask

What is Autoindex in nginx?

Definition of Nginx Autoindex. Nginx autoindex is used the ngx http autoindex module to process the requests which was ending with the “/” slash character and it will produce the listing directory. Basically, the request is passed by using the ngx http autoindex module when the specified module will not find the index.

How do I use nginx as a file server?

Serving static files using nginx as web server is a good option. For making the static files available you need to copy your testfolder to /usr/share/nginx/html inside the nginx image. After which you will be able to see the files on your browser on port 8080.


1 Answers

You just need to return HTTP header Content-disposition:

location ~* /orig/(.+\.jpg)$ {
    add_header Content-disposition "attachment; filename=$1";
}
like image 151
Alexander Azarov Avatar answered Oct 22 '22 12:10

Alexander Azarov