How to force file download in the browser, nginx server

前端 未结 4 1380
误落风尘
误落风尘 2021-01-04 05:38

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         


        
4条回答
  •  别那么骄傲
    2021-01-04 06:20

    I have been trying to get this functionality into my own nginx server.

    I have a folder on server that I would like to have open for anyone who knows the path.

    To do that I added a /file/ location into my nginx config file.

    server {
       listen 80;
       server_name example.com;
    
       location /file/ {
           alias /var/openStuff/file/;
           add_header Content-disposition "attachment";
       }
    }
    

    Any request to example.com/file/x will go to /var/openStuff/file and look for x and force the browser to download the file

    This will also work if the path is /file/stuff/things.txt and will serve it from /var/openStuff/file/stuff/things.txt

提交回复
热议问题