Nginx 413 Request Entity Too Large: Resolving `client_max_body_size` on Debian 12 Bookworm
Fix Nginx 413 'Request Entity Too Large' errors on Debian 12. This guide shows how to adjust `client_max_body_size` to allow larger file uploads on Bookworm.
Fix Nginx 413 'Request Entity Too Large' errors on Debian 12. This guide shows how to adjust `client_max_body_size` to allow larger file uploads on Bookworm.
When users attempt to upload files through a web application powered by Nginx on Debian 12, they might encounter a "413 Request Entity Too Large" error. This frustrating message indicates that the file size exceeds a predefined limit configured within Nginx, preventing the successful completion of the upload. This guide will walk you through diagnosing and resolving this common issue by adjusting Nginx's client_max_body_size directive, and address related PHP settings if your application relies on PHP-FPM.
Symptom & Error Signature
The most immediate symptom is a web browser displaying an Nginx 413 error page when a large file is uploaded.
Browser Output:
413 Request Entity Too Large
nginx/1.22.1
Nginx Error Log Entry (typically found in /var/log/nginx/error.log):
2023/10/26 10:30:45 [error] 1234#1234: *5 client intended to send too large body: 12345678 bytes, client: 192.168.1.1, server: example.com, request: "POST /upload HTTP/1.1", host: "example.com"
The error log entry clearly states "client intended to send too large body," followed by the size in bytes that exceeded the limit, the client's IP, the server name, the request method, and the requested path.
Root Cause Analysis
Nginx, by default, imposes a limit on the size of the client request body. This limit is controlled by the client_max_body_size directive, which is typically set to 1m (1 Megabyte) out-of-the-box. This default is a security and resource-management measure, designed to prevent potential denial-of-service (DoS) attacks where an attacker could flood the server with excessively large requests, consuming resources and impacting availability.
When a web application attempts to send a POST request with a body larger than the configured client_max_body_size, Nginx will immediately terminate the request, discard the connection, and return a 413 HTTP status code to the client. This issue commonly arises when users try to upload images, videos, documents, or other sizable files through content management systems (CMS), forums, or custom web applications without the Nginx configuration being adjusted to accommodate larger sizes.
It's also crucial to understand that if your web application uses a backend language like PHP (via PHP-FPM), PHP itself has its own independent limits for file uploads (upload_max_filesize) and POST data (post_max_size). For a large file upload to succeed, all these limits (Nginx and PHP) must be set to a value greater than or equal to the size of the file being uploaded.
Step-by-Step Resolution
Follow these steps to increase the allowed request body size in Nginx and, if necessary, in PHP-FPM.
1. Locate Nginx Configuration Files
Nginx configurations are highly modular. The client_max_body_size directive can be placed in different contexts: http, server, or location blocks. The most common files on Debian 12 include:
/etc/nginx/nginx.conf: The main global configuration file./etc/nginx/conf.d/*.conf: Directory for additional global configuration snippets./etc/nginx/sites-available/your_domain.conf: Configuration files for individual virtual hosts (symlinked tosites-enabled/).
You should typically modify the configuration within the server block corresponding to your website or, if the limit needs to apply globally, within the http block in nginx.conf.
2. Modify client_max_body_size
Choose the appropriate scope for your change.
Option A: Global setting (affecting all sites)
Edit /etc/nginx/nginx.conf and place the directive inside the http { ... } block.
sudo nano /etc/nginx/nginx.conf
Add or modify the client_max_body_size directive:
http {
# ... other directives ...
client_max_body_size 50M; # Set to your desired maximum size, e.g., 50 Megabytes
# ...
}
Option B: Per-site setting (recommended for specific needs)
Edit the configuration file for your specific site, typically located in /etc/nginx/sites-available/your_domain.conf.
sudo nano /etc/nginx/sites-available/your_domain.conf
Add or modify the client_max_body_size directive inside the server { ... } block:
server {
listen 80;
listen [::]:80;
server_name your_domain.com www.your_domain.com;
client_max_body_size 50M; # Set this site's maximum upload size
# ... other directives ...
}
Option C: Per-location setting (for specific URLs or paths)
If you only need to increase the limit for a particular upload endpoint (e.g., /upload), you can place the directive within a location block. This directive takes precedence over server and http block settings for that specific location.
sudo nano /etc/nginx/sites-available/your_domain.conf
server {
# ...
location /upload {
client_max_body_size 100M; # Specific limit for /upload endpoint
# ...
}
# ...
}
- The value for
client_max_body_sizecan be specified in bytes, kilobytes (k), or megabytes (m). For example,100kfor 100 KB, or50mfor 50 MB.- Choose a size that is appropriate for your application's needs but avoid excessively large values, as this could still expose you to resource exhaustion if not managed properly by the application itself.
3. Verify Nginx Configuration Syntax
Before restarting Nginx, always test the configuration for syntax errors. This prevents service downtime due to misconfigurations.
sudo nginx -t
You should see output similar to this:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If you see any errors, review your recent changes carefully and correct them.
4. Restart Nginx Service
Once the configuration syntax is verified, restart Nginx to apply the changes:
sudo systemctl restart nginx
To confirm Nginx is running without issues:
sudo systemctl status nginx
Ensure the status is active (running).
5. (Optional but Recommended) Adjust PHP Configuration (if applicable)
If your web application uses PHP-FPM, you must also adjust PHP's own upload limits, as they can also cause files to be rejected, often without a direct Nginx 413 error (sometimes a 200 OK with an empty file or a different error depending on the application logic).
You need to modify upload_max_filesize and post_max_size in your php.ini file. For Debian 12, PHP 8.2 is common.
Locate
php.ini: The exact path depends on your PHP version and SAPI. For PHP-FPM 8.2 on Debian 12, it's typically/etc/php/8.2/fpm/php.ini. You can confirm this with:php -i | grep "Loaded Configuration File"Edit
php.ini:sudo nano /etc/php/8.2/fpm/php.iniFind and adjust the following directives to match or exceed your Nginx
client_max_body_size. Remember thatpost_max_sizeshould be equal to or greater thanupload_max_filesize.upload_max_filesize = 50M post_max_size = 50MEnsure
post_max_sizeis at least as large asupload_max_filesize. It's common practice to set them to the same value, matching your Nginxclient_max_body_size.Restart PHP-FPM Service:
After modifying
php.ini, you must restart the PHP-FPM service for the changes to take effect:sudo systemctl restart php8.2-fpmVerify its status:
sudo systemctl status php8.2-fpm
6. Test the Upload
Now, attempt to upload the file through your web application again. The large file upload should now succeed without the Nginx 413 error.
7. Consider Proxy Buffering (Advanced)
If Nginx is acting as a reverse proxy for an upstream application server (e.g., Node.js, Python/Gunicorn, Tomcat), and you are still encountering issues with very large files despite adjusting
client_max_body_size, you might need to look into Nginx's proxy buffering directives. Whileclient_max_body_sizedirectly handles the initial client request size, large responses or very long requests from the upstream server might involve buffering.Common proxy directives related to buffering include:
proxy_request_buffering off;(disables buffering of client request body, sending it directly to upstream)proxy_buffering on;(usually on by default, buffers responses from upstream)proxy_max_temp_file_size 0;(disables writing large buffered responses to temporary files on disk)proxy_temp_file_write_size 8m;(maximum amount of data Nginx will write to a temporary file at one time)For most 413 errors caused by file uploads,
client_max_body_sizeis the primary solution, but keep proxy buffering in mind for more complex reverse proxy scenarios.
By carefully following these steps, you should successfully resolve the "Nginx 413 Request Entity Too Large" error on your Debian 12 Bookworm server, allowing for larger file uploads on your web applications.
Our Production Verification Guarantee
Encountering a bug not covered here or running a non-standard kernel configuration? Our solutions are continually refined against real production incidents. Submit an environment trace for our editorial team to replicate.