Solve the file corruption caused by nginx downloading large files, and the file size is inconsistent

Source: Internet
Author: User
: This article mainly introduces how to solve the file corruption caused by large files downloaded by nginx, and the file size is inconsistent. if you are interested in the PHP Tutorial, please refer to it. The partner reported a problem today, saying that a 2 MB file was uploaded on the webpage, which was only 64 KB during webpage download and failed to be opened. After confirming that the BUG exists and must exist, I set foot on the path to debugging and solving the BUG.

1. the system is nginx + php + mysql, which is irrelevant to mysql based on experience. you can ignore TA.
2. after uploading a 2 MB file from the PHP webpage, open the file directly on the server and check the file properly. the file is the same as the binary file of the original file.
3. using different browsers and computers to repeatedly download the file from the PHP web page, we found that the downloaded files are only 64 KB.
4. there is no exception when uploading or downloading a file with a size of only 90 kB from the PHP webpage.
Based on the above four points, it can be basically determined that the problem lies in nginx. Open the nginx log file and find the following error log,
[Crit] 21636 #0: * 843968 open () "/home/www/local/nginx/fastcgi_temp/0/11/0000000110" failed (13: Permission denied) while reading upstream, .....
You can make a bold guess that the normal file cannot be obtained because you do not have sufficient permissions to operate the fastcgi_temp folder. Therefore, after assigning permissions to the folder, the problem is solved.

Why?

View the nginx configuration file and find the following section:
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
** Fastcgi_buffer_size 64 k;
Fastcgi_buffers 4 64 k ;**
Fastcgi_busy_buffers_size 128 k;
Fastcgi_temp_file_write_size 128 k;
The file size is always 64 KB each time the download fails. it should be related to this. Originally, nginx uses the buffer size specified by fastcgi_buffer_size to cache the content of the fastcgi stream. When the size exceeds this size, the request buffer will be applied with the quantity and size specified by fastcgi_buffers. If the size is still exceeded, the excess content will be written to the temporary file. That is to say, in this case, nginx will first use a 64 K buffer to buffer the first part of the fastcgi stream, and then apply for a buffer of up to 4*64 K = 256K for caching. If the limit is exceeded, a temporary file is written. Therefore, when downloading a file larger than k, you need to use a temporary folder to buffer the file. However, you do not have the permission to perform operations here, which causes this problem.

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above describes how to solve the problem of file corruption and inconsistent file size when downloading large files in nginx, including some content. I hope to help anyone who is interested in PHP tutorials.

    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.