If your upstream server that nginx reverse-proxies to supports streaming uploads of large files, it makes most sense to simply disable nginx buffering these uploads at all, using:
proxy_request_buffering off;
proxy_http_version 1.1;
client_max_body_size 0;
In case you also like to copy comments into your config:
# Explanation:
# proxy_request_buffering off; -- upstream server can hanle large streaming uploads
# proxy_http_version 1.1; -- required to disable request buffering also for clients that send chunked encoding
# client_max_body_size 0; -- otherwise nginx imposes a size restriction; only upstream should decide that
Directive details
Rationale for disabling request buffering
There is not point in nginx storing the request in such case:
- It delays your upstream application seeing it.
- This also means it cannot return errors early, such as "permission denied"; it's bad UX for the user to learn this only after waiting for a 30 minute upload.
- It unnecessarily consumes additional disk space.
- That introduces unexpected unreliability: People may expect that for a "passthrough-only" proxy server, only network and maybe RAM are important; they do not expect that requests will fail if the disk is too small.
- Disk IO is often slow.
Buffering the entire request body is designed for upstream applications that cannot handle slow streaming uploads (e.g. PHP scripts with a maximum runtime duration).
If your upstream app does not have such a restriction, you do not need request buffering to work around it.
Further links
See also this comment for a more detailed explanation of how nginx does proxy buffering, in particular proxy_request_buffering
.
In a link I put in there, an nginx developer explains why for (client <- nginx) connections, some caching can still be beneficial.
But for (nginx <- upstream), "some caching" is not currently possible, as proxy_request_buffering
always fully buffers the entire client request and does no streaming at all.
This post describes proxy buffering in the other direction (client <- nginx): https://www.getpagespeed.com/server-setup/nginx/tuning-proxy_buffer_size-in-nginx
Check out especially the section Disable proxy buffering?, which has a similar argument as the one I have for (nginx -> proxy) above.