Nginx – Optimizing nginx client_body_buffer_size for web app accepting file uploads

nginxoptimization

When configuring nginx, what considerations affect client_body_buffer_size?

I've set it to 10m for a website where users upload photos and image-memes (kind of like 9gag). Note that client_max_body_size is set to 10m as well. Overall, my reasoning is that the webserver should accept POST requests with images as big as 10MB (website policy) – and thus both these directives ought to be 10m.

Is this reasoning correct?

Can someone shed light on the factors affecting what values to set for client_body_buffer_size, and typical values of client_body_buffer_size (for the sort of web application I described)?

In case it matters, I utilize nginx as a reverse proxy.

Best Answer

The documentation states what client_body_buffer_size does:

Sets buffer size for reading client request body. In case the request body is larger than the buffer, the whole body or only its part is written to a temporary file. By default, buffer size is equal to two memory pages. This is 8K on x86, other 32-bit platforms, and x86-64. It is usually 16K on other 64-bit platforms.

This memory is in use only while a request is being uploaded; once it is passed on to the backend, the memory is freed again. If a request is larger than this value, it will go to a temporary file and a warning will appear in the error log.

This means that you must have enough RAM to store any simultaneous uploads, or your server will begin swapping.

The performance impact of this is probably not that great, unless you are severely memory constrained and getting lots of uploads at the same time.