Project actual combat typical case 28 - the disaster caused by nginx limit upload size in production environment

1: Background introduction

This blog is a summary and improvement of the disaster caused by the limit upload size of nginx in the production environment .
The purpose is to transform the experience into your own. Share it with everyone through the blog, and we will make progress and improve together.
insert image description here

Two: ideas & solutions

It is a very scary solution to adjust the size of nginx uploaded files in a production environment:

  1. First of all, because it is a production environment, there are likely to be users who are using it. No matter whether the modification is successful or not, the nginx service needs to be restarted, which will cause the service to be unavailable for a certain period of time.
  2. This plan does not take into account subsequent changes. If you adjust to upload documents of 1.5 million people this time, what will you do if next time I am a document of 2.5 million people? Do you want to modify the size of nginx uploaded files again?

plan

Is it absolutely impossible to modify the size of nginx uploaded files? I don't think so.
The above solution (directly modify nginx upload file size) does not consider other situations 1. Is it appropriate to modify nginx now, and what are the advantages and disadvantages? 2. Are there any other better solutions? What are their advantages and disadvantages?

Solution to nginx limit upload size in production environment

Background: There are likely to be users who are using the body test in the production environment, but now the college needs to import the excel sheets corresponding to 1.50,000 people, but due to the restrictions on nginx, the import of 1.50,000 excel sheets failed.

  1. It cannot affect the use of online users, so we can enter the form of 1.5w people to check people, import in 5 batches, and import the form of 3000 people. Find that nginx can accept the maximum number of uploads.

For uploading files later, here is how to design to avoid today's situation

  1. Considering the future application scenarios at the beginning of the design, set the limit of nginx upload file size and the timeout time of nginx interface in advance
  2. The front end of the page sets the above file size limit. If it prompts how large a file is supported for uploading, it is recommended to upload a large file.
  3. The backend can be used to import excel tables in batches, and multi-threading can be used to save import time.

Three: Nginx upload file size configuration

The size of the uploaded file is set in the nginx configuration file nginx.conf.

Nginx generally defaults to uploading a maximum request body of 1m. If set to 0, the upload file size is unlimited.

Default: client_max_body_size 1m;
 

nginx supports setting client_max_body_size in http block, server block and location block

The main difference between the three is: http{} controls the request received by nginx, and server{} controls the size of the request packet received by the server; location{} only takes effect for the request that matches the location routing rule.

example

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    #默认的上传最大请求体在1m。如果设置为0,表示上传文件大小不受限制。
    client_max_body_size 10m;  
    include /etc/nginx/conf.d/*.conf;
}

Four: Summary

  1. For the solution to the problem, we can't think of only one solution. We need to get along with more than 3 solutions, and evaluate which one is more suitable in the current environment.
  2. Adhere to the thinking of choosing two, choosing two, choosing three

Guess you like

Origin blog.csdn.net/wangwei021933/article/details/129540308