• notice
  • Congratulations on the launch of the Sought Tech site

The picture display is too slow, the file download is incomplete, it turns out to be a Nginx problem

write in 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 fully loaded in the browser. down. And the strange thing is that the reader's network is fine. So, I started to help him troubleshoot all kinds of problems. . .

identify the problem

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

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

Using the IP address of the background server to access directly on the background server, the speed is found to be quite fast, so I suspect that it is a configuration problem of Nginx.

Note: When downloading a large attachment or a large picture on the page, the download will be interrupted or the picture 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 website doesn't have large files, at least not so large that they can't be loaded with Nginx's default configuration.

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

Some of the important parameters are shown below.

  • proxy_connect_timeout 600; #nginx connects to the backend server time out (the proxy connection time out)

  • proxy_read_timeout 600; #After the connection is successful, the backend server response time (the proxy receives the timeout)

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

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

  • proxy_buffers 4 32k; #proxy_buffers buffer, if the average web 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; #Set the size of the cache folder, larger than this value, it will be transmitted from the upstream server

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

proxy_temp_file_write_size 16k;

And the pictures on his server are basically between 100K~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 (default is the /proxy_temp directory under the Nginx installation directory), and by default Nginx is started as nobody , Use the ls -al command to check that the proxy_temp directory nobody is the owner of the proxy_temp directory. Why is there no permission? Next, look at the parent directory of proxy_temp, which is the Nginx installation directory. It is found that nobody competes without permission, no wonder the above problems occur.

Solve the problem

Once the problem is identified, it is easier to solve the problem. This problem can be solved in two ways, as shown below.

  • Set the proxy_temp directory that anyone can write, and restart Nginx to solve it.

  • Change the value of proxy_temp_file_write_size directly, modify it 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, use the following command to set the /usr/local/nginx/proxy_temp directory to be writable by anyone, and the problem will be solved.

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 optimized some other configuration items for this reader.


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+