Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of slow picture display and incomplete file download caused by Nginx

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve the problem of too slow picture display and incomplete file download caused by Nginx". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn together "how to solve the problem of too slow picture display and incomplete file download caused by Nginx"!

problem location

After a series of investigations (I omitted the middle process and wrote the key points directly!), Finally, the problem was identified as Nginx. When I opened the admin system of this reader's website, I found that the image displayed very slowly, and found the following error message on the Nginx front-end proxy.

[error] 28423#0: *5 connect() failed (111: Connection refused) while connecting to upstream

Directly on the background server with the IP address of the background server to access, found that the speed is quite fast, so suspected that Nginx configuration problems.

Note: When downloading large attachments, or when there are large images in the page, the download will be interrupted or the image cannot be displayed. Maybe you will say that the default configuration of Nginx I use has never encountered such a problem! What I'm trying to say is: That's because your site doesn't have large files, at least not so large that they won't load using Nginx's default configuration.

Here, I give a section of Nginx configuration, as follows.

location /file { root /home/file; index index.html index.htm; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080 ; client_max_body_size 100m; client_body_buffer_size 128k; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 32k; proxy_buffers 4 64k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; }

A few of the important parameters are listed below.

proxy_connect_timeout 600; #timeout for nginx connection to backend server (proxy connection timeout)

proxy_read_timeout 600; #backend server response time after successful connection (proxy receive timeout)

proxy_send_timeout 600; #Backend server data return time (proxy send timeout)

proxy_buffer_size 32k; #Set the buffer size of proxy server (nginx) to store user header information

proxy_buffers 4 32k; #proxy_buffers buffer, if the average page is below 32k, set it like this

proxy_busy_buffers_size 64k; #Buffer size under heavy load (proxy_buffers*2)

proxy_temp_file_write_size 16k; #Set cache folder size, larger than this value, will be transmitted from upstream server

See here, found the problem, this reader's Nginx has the following line configuration.

proxy_temp_file_write_size 16k;

The images on his server are basically between 100K and 5M.

The problem lies in proxy_temp_file_write_size. When the file size on the server exceeds the size set by this parameter, Nginx will first write the file to the temporary directory (default is Nginx installation directory/proxy_temp directory). By default, Nginx is started as nobody. Use ls -al command to check proxy_temp directory. Nobody is the owner of proxy_temp directory. Strange. Why not have permission? Next look at the parent directory of proxy_temp, which is the Nginx installation directory. No wonder nobody has no permission, no wonder the above problem appears.

solve problems

Once the problem was located, it would be easier to solve it. There are two ways to solve this problem, as shown below.

Anyone can write proxy_temp directory, restart Nginx can solve.

Change proxy_temp_file_write_size to be larger than image and file size, restart Nginx.

If you solve the problem in the first way, for example, my proxy_temp directory is/usr/local/nginx/proxy_temp, use the following command to set the/usr/local/nginx/proxy_temp directory so that anyone can write to it.

chmod -R 777 /usr/local/nginx/proxy_temp/

If you are using the second method to solve the problem, you can directly modify the nginx.conf file, as shown below.

location /file { root /home/file; index index.html index.htm; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080 ; client_max_body_size 100m; client_body_buffer_size 256k; proxy_connect_timeout 1200; proxy_read_timeout 1200; proxy_send_timeout 6000; proxy_buffer_size 32k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 10m; } Thank you for reading, the above is "how to solve the image caused by Nginx is too slow, file download incomplete problem" content, after learning this article, I believe we have a deeper understanding of how to solve the image caused by Nginx is too slow, file download incomplete problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report