File upload limit in Nginx & (The server returned a "413 Content Too Large")
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
File Upload Limits in Nginx: Decoding the "413 Content Too Large" Error
As developers, dealing with file uploads often involves wrestling with server configurations. You might successfully configure your web server settings, yet inevitably run into cryptic errors like the HTTP 413 "Content Too Large," even when you've set limits in your Nginx configuration. This post will dive deep into why this happens and provide a comprehensive, developer-focused solution.
Understanding the 413 Content Too Large Error
The HTTP status code 413 Payload Too Large is sent by the server when the request payload (the data being sent to the server, in this case, the uploaded file) exceeds the limit configured on the server or proxy layer. While you correctly set client_max_body_size in Nginx, if the upload fails, it often indicates that another layer—specifically the application runtime environment—is enforcing a stricter limit first.
The Nginx Perspective: client_max_body_size
In Nginx, the directive responsible for setting the maximum allowed size of the client request body is indeed client_max_body_size. When dealing with file uploads, this is the primary line of defense on the web server side.
Here is how you typically configure it within your HTTP or server blocks:
http {
# Setting the limit to 100 Megabytes for all requests
client_max_body_size 100M;
server {
listen 80;
server_name example.com;
location /upload/ {
# Ensure this directive is present in your location block if needed,
# though placing it at the http level often suffices for general limits.
client_max_body_size 100M;
# ... other upload configurations
}
}
}
If you have implemented this correctly and are still receiving a 413 error, the problem almost certainly lies in the interaction between Nginx and the backend application language, typically PHP.
The Crucial Interaction: Nginx vs. PHP Limits
The most common reason for this discrepancy is that the PHP execution environment imposes its own constraints on how much data it can process before handing control back to Nginx or the client. If the file size you attempt to upload exceeds the limits set by PHP, PHP will reject the request before Nginx even fully processes the request body, resulting in an error that manifests as a 413.
For frameworks like Laravel, which heavily rely on PHP for processing uploads and routing files, these PHP settings must be equally adjusted. You need to ensure three related directives are harmonized:
upload_max_filesize: The maximum size of an uploaded file.post_max_size: The maximum size of POST data that PHP will accept. This value must be greater thanupload_max_filesize.memory_limit: The amount of memory allocated to the script, which must also be sufficient for handling large files.
You must edit your PHP configuration file (e.g., php.ini) and adjust these values accordingly:
upload_max_filesize = 100M
post_max_size = 105M ; Must be slightly larger than upload_max_filesize
memory_limit = 256M ; Ensure enough memory for processing large files
Best Practice Note: When building robust applications, understanding this stack is vital. Frameworks like Laravel rely on a stable and predictable environment. Ensuring that your web server (Nginx) and the backend runtime (PHP/FPM) are all configured with compatible limits prevents these frustrating upstream errors. This layered approach to configuration is key to creating scalable systems, much like designing solid architectural patterns when you build out a solution on platforms like Laravel Company.
Conclusion: The Unified Solution
To successfully handle large file uploads without encountering the 413 error, you must treat the request pipeline as a whole. Do not rely on a single configuration point.
The fix is harmonization: Set the limits in Nginx (client_max_body_size) and ensure they are safely exceeded by the corresponding settings in your PHP configuration (post_max_size and upload_max_filesize). By aligning these two critical layers, you ensure that the file size limit is enforced consistently across the entire stack, giving you reliable, large-scale file handling.