Usually when you are running a site where the user uploads a lot of files, you will see that when they upload a file which is more than 1MB in size you get an Nginx error stating, "Request entity too Large" (413), as shown in the following screenshot. We will look at how Nginx can be configured to handle larger uploads.
This is controlled by one simple part of the Nginx configuration. You can simply paste this in the server part of the Nginx configuration:
client_max_body_size 100M; # M stands for megabytes
This preceding configuration will allow you to upload a 100 megabyte file. Anything more than that, and you will receive a 413. You can set this to any value which is less than the available disk space to Nginx, which is primarily because Nginx downloads the file to a temporary location before forwarding it to the backend application.
Nginx also lets us control other factors related to people uploading files on the web application, like timeouts in case the client has a slow connection. A slow client can keep one of your application threads busy and thus potentially slow down your application. This is a problem that is experienced on all the heavy multimedia user-driven sites, where the consumer uploads all kinds of rich data such as images, documents, videos, and so on. So it is sensible to set low timeouts.
client_body_timeout 60; # parameter in seconds client_body_buffer_size 8k; client_header_timeout 60; # parameter in seconds client_header_buffer_size 1k;
So, here the first two settings help you control the timeout when the body is not received at one read-step (basically, if the server is queried and no response comes back). Similarly, you can set the timeout for the HTTP header as well. The following table lists out the various directives and limits you can set around client uploading.
Directive |
Use |
---|---|
This directive forces Nginx to always store a client request body in temporary disk files, even if the file size is 0. The file will not be removed at request completion. | |
This directive specifies whether to keep the whole body in a single client request buffer. | |
This directive specifies the client request body buffer size. If the request body is more than the buffer, then the entire request body or some part is written in a temporary file. | |
This directive assigns the directory for storing the temporary files in it with the body of the request. | |
This directive sets the read timeout for the request body from client. | |
This directive sets the header buffer size for the request header from client. | |
This directive assigns timeout with reading of the title of the request of client. | |
This directive assigns the maximum accepted body size of client request, indicated by the line Content-Length in the header of request. |