Nginx file forced Download configuration method _nginx

Source: Internet
Author: User

Today's browsers can recognize many file types (txt,pdf,jpg ...) and automatically open them in the browser

Situation One:
Add header information content-disposition "attachment;" Causes the browser to force the download:

Copy Code code as follows:

#表示浏览器内嵌显示一个文件
Content-disposition:inline; Filename=foobar.pdf

#表示会下载文件, such as the Firefox browser
Content-disposition:attachment; Filename=foobar.pdf

The Nginx configuration is as follows, adding the following location to the appropriate server:

Copy Code code as follows:

Location/upload {
Add_header content-disposition "attachment;";
}

Situation Two:

There is such a demand, for picture files and PDFs and other file links, as long as access is really a path, can not open the picture in the browser, but prompts the user to save to local, file name using access to the file name.
This problem is mainly caused by IE, no matter what MIME type, such as manually set the MIME type of the image to Octet-stream, if the browser to know the file suffix, still open the picture in the browser, ie browser enough rubbish?

Solution:

In response to the HTTP header add: content-disposition:attachment; Filename= file name
The Nginx configuration is as follows:

Copy Code code as follows:

Location ~ ^/somepath/(. *) $ {
Add_header content-disposition "attachment; Filename=$1 ";
Alias "E:/apache-tomcat-7.0.32/webapps/upload/$1";
}

A regular expression is used here to capture the file name of the request.
In addition, need to pay attention to Nginx location priority, First is =, then is ^~, finally is ~.
In this way, IE browser will ignore the original MIME type, prompting the user to save the file to the local.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.