nginx solves the problem of slow picture display and incomplete download

  • 2021-08-21 21:48:01
  • OfStack

Write at the front

Recently, a reader told me that when he accessed his server through a browser, the pictures were displayed so slowly that they could not be completely loaded in the browser, and when he downloaded the files, he was even more annoyed, and the files could not be completely downloaded at all. And it is strange that this reader's network is fine. So, I began to help him troubleshoot various problems. . .

Problem orientation

After 1 series of investigation (I omitted the intermediate process and wrote the key points directly!) Nginx is finally located as a problem. When I opened the reader's website back-end management system and found that the picture display was very slow, I found the following error message on the Nginx front-end agent.


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

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

Note: When you download a large attachment or have a large picture in the page, the download will be interrupted or the picture will not be displayed. Maybe you will say that the default configuration of Nginx I used has never encountered this problem! What I want to say is: That's because your website doesn't have big files, at least not big enough to load with the default configuration of Nginx.

Here, I give a configuration of Nginx, 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 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;
}

Several important parameters are shown below.

proxy_connect_timeout 600; # nginx connection timeout with back-end server (proxy connection timeout) proxy_read_timeout 600; # Back-end server response time after successful connection (proxy receive timeout) proxy_send_timeout 600; Back-end server data backhaul time (proxy send timeout) proxy_buffer_size 32k; # Sets the buffer size of the proxy server (nginx) to hold 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 high load (proxy_buffers*2) proxy_temp_file_write_size 16k; # Sets the cache folder size, larger than which will be transferred from the upstream server

Seeing this, the problem is found. The reader's Nginx has the following 1 line configuration.


proxy_temp_file_write_size 16k;

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

The problem lies in proxy_temp_file_write_size. When the file on the server exceeds the size set by this parameter, Nginx will first write the file to the temporary directory (the default is the directory under Nginx installation /proxy_temp). The default Nginx is started as nobody. Check the proxy_temp directory with ls-al command. nobody is the owner of proxy_temp directory. Next, look at the parent directory of proxy_temp, the Nginx installation directory. It is found that nobody has no authority, so it is no wonder that the above problems will occur.

Solve a problem

Locate the problem, and then solve the problem is relatively simple. There are two ways to solve this problem, as shown below.

Set anyone can write proxy_temp directory, restart Nginx can be resolved. Directly change the value of proxy_temp_file_write_size to be larger than the size of the picture and file, and restart Nginx.

If the problem is solved in the first way, for example, my proxy_temp directory is /usr/local/nginx/proxy_temp, and the problem is solved by setting the directory /usr/local/nginx/proxy_temp to be writable by anyone with the following command.


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

If you use 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;
}

Of course, I also helped this reader optimize 1 other configuration items.

Ok, let's call it a day! Don't forget to praise, give it to someone who is watching and forwarding it, so that more people can see it, one study, one progress! !

The above is nginx to solve the problem of slow picture display and incomplete download details, more about nginx to solve the problem of slow picture display and incomplete download, please pay attention to other related articles on this site!


Related articles: