1. Default value and error prompts
-
default value:
client_max_body_size 1m;
- The maximum request body allowed by Nginx is1 MiB, exceeding this value will return413 Request Entity Too Largemistake.
- Error prompt example:
HTTP/1.1 413 Request Entity Too Large
Content-Type: text/html
2. How to configure client_max_body_size
This command can be placed inhttp
、server
orlocation
In the block, the latter will override the former configuration:
http { # Global settings, acting on all servers client_max_body_size 50m; server { listen 80; server_name ; # Set it separately for a virtual host client_max_body_size 100m; location /upload { # Recover for specific paths client_max_body_size 200m; proxy_pass http://backend; } } }
illustrate: declared in nested block
client_max_body_size
Will overwrite the command cite turn1search1 in the ancestor block.
unit:supportk
、m
、g
(Case insensitive), such as10K
、2m
、1G
。
3. The maximum upper limit of client_max_body_size
Nginx internally stores the value as a C language typeoff_t
, its position width determines the upper limit:
64-bit executable file
-
off_t
It is a 64-bit signed integer with a theoretical maximum of 2⁶³ − 1 byte (approximately 9.22×10¹⁸ bytes, ≈ 8 EiB), so it can be used to convert theclient_max_body_size
Set to2^63
Without errors.
32-bit executable file (large file support is not enabled)
-
off_t
is a 32-bit signed integer with a maximum value of about 2³¹ − 1 (approximately 2 GiB), i.e.2147483647byte. If you need to break this limit, you can define it at compile time_FILE_OFFSET_BITS=64
Enable large file support to enableoff_t
Expanded to 64.
4. Cancel the size check (unlimited upload)
Willclient_max_body_size
Set as0
,CanDisabledCheck the size of the request body, allowing unlimited uploads or POST requests:
server { listen 80; server_name ; # Cancel size limit client_max_body_size 0; location / { proxy_pass http://backend; } }
illustrate: Set to
0
When Nginx no longer intercepts any size request body at its own level.
5. Practical examples
Relax single interface restrictions as needed
server { listen 80; server_name ; # 10MiB is allowed by default client_max_body_size 10m; location /api/v1/upload { # Single interface relaxed to 500MiB client_max_body_size 500m; proxy_pass http://backend_upload; } }
Turn on unlimited upload
server { listen 80; server_name ; # Completely cancel the size limit at the Nginx level client_max_body_size 0; location /files { proxy_pass http://backend_files; } }
6. Configuration Verification
# Check configuration syntaxnginx -t # Overload Nginx (apply new configuration)nginx -s reload
It can be used latercurl
Or upload large files on the front end to confirm whether they still appear413
Error, and view/var/log/nginx/
To troubleshoot problems.
7. Summary
-
Core Instructions:
client_max_body_size
Determines the upper limit of request body size at the Nginx level. -
default value:1 MiB(
1m
)。 -
Maximum upper limit:Depend on
off_t
Bit width determines, 64-bit environment supports up to 2⁶³ − 1 byte, 32-bit environment (large file support is not enabled) with a limit of approximately 2 GiB. -
Cancel restrictions: Set to
0
You can disable checking and allow unlimited uploads.
With the above configuration and examples, you can flexibly adjust or remove Nginx's upload size limits to meet various business needs from small forms to oversized file transfers.
This is the article about the Nginx file upload size limit and client_max_body_size maximum value record. For more related content on nginx file upload size limit, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!