Fixing Laravel 413 Request Entity Too Large & PostTooLargeException (Nginx, PHP-FPM & Cloudflare)
When users upload PDFs, high-resolution images, CSV imports, or video assets to a production Laravel application, uploads that worked locally often fail with one of two responses depending on which layer rejected the payload first.
If Nginx rejects the request before it ever reaches PHP, the browser receives:
413 Request Entity Too Large
nginx/1.26.2
If the request passes Nginx but exceeds PHP’s configured post_max_size, Laravel’s ValidatePostSize middleware aborts the request with:
Illuminate\Http\Exceptions\PostTooLargeException
The POST data is too large.
Because a file upload travels through up to four separate layers—CDN/proxy, Nginx, PHP-FPM, and Laravel validation—raising the limit in only one place leaves the upload broken.
Quick Diagnostic Reference
| Symptom | Failing Layer | Solution |
|---|---|---|
Plain HTML 413 Request Entity Too Large page from nginx |
Nginx defaults to a 1M body cap |
Increase client_max_body_size in Nginx (Step 1) |
Illuminate\Http\Exceptions\PostTooLargeException in Laravel |
PHP’s post_max_size is smaller than the uploaded payload |
Raise upload_max_filesize and post_max_size in php.ini (Step 2) |
| Uploads under 100 MB succeed, but files over 100 MB fail behind Cloudflare | Cloudflare Free and Pro plans enforce a hard 100 MB HTTP body limit | Switch to chunked uploads or direct S3/R2 pre-signed URLs (Step 3) |
Server returns 422 Unprocessable Content instead of 413 |
Laravel max: validation rule (in kilobytes) or Livewire temp upload cap |
Fix validation rules and config/livewire.php (Step 4) |
Step 1: Increase client_max_body_size in Nginx
By default, Nginx rejects any request body larger than 1 MB. You can raise this limit globally inside /etc/nginx/nginx.conf (http block) or specifically inside your site’s server block (for example, /etc/nginx/sites-available/your-app.conf):
server {
listen 443 ssl http2;
server_name example.com;
root /var/www/html/public;
# Allow uploads up to 64 MB
client_max_body_size 64M;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
Test the Nginx syntax and reload the worker processes without dropping active connections:
sudo nginx -t && sudo systemctl reload nginx
If you run a second Nginx reverse proxy container in front of your Laravel container in Docker, set client_max_body_size in both Nginx configs.
Step 2: Raise upload_max_filesize and post_max_size in PHP-FPM
Once Nginx forwards the request, PHP-FPM enforces its own limits. Remember two rules that trip up almost every deployment:
post_max_sizemust be larger thanupload_max_filesizebecause multipart form payloads include boundary overhead and additional form fields alongside the file.- CLI and PHP-FPM use different
php.inifiles. Editing/etc/php/8.3/cli/php.inidoes nothing for web requests.
On Ubuntu / Debian VPS (PHP-FPM)
Find your active FPM configuration file and update the upload directives:
sudo nano /etc/php/8.3/fpm/php.ini
Set these values (adjusting sizes to match your Nginx limit):
upload_max_filesize = 50M
post_max_size = 64M
memory_limit = 256M
max_execution_time = 120
Restart PHP-FPM so the new settings take effect:
sudo systemctl restart php8.3-fpm
Inside Docker (Laravel Sail or Custom Dockerfile)
Mount or copy a dedicated .ini override file into /usr/local/etc/php/conf.d/uploads.ini so PHP loads it automatically on container startup:
; docker/php/uploads.ini
file_uploads = On
upload_max_filesize = 50M
post_max_size = 64M
memory_limit = 256M
max_execution_time = 120
Copy it inside your Dockerfile:
COPY docker/php/uploads.ini /usr/local/etc/php/conf.d/uploads.ini
Rebuild and restart the container, then verify PHP-FPM sees the new values:
docker compose exec app php -i | grep -E "upload_max_filesize|post_max_size"
Step 3: Bypass Cloudflare’s 100 MB Request Limit
If your domain is proxied through Cloudflare (orange cloud enabled), Cloudflare enforces a strict per-request upload cap:
- Free & Pro plans: 100 MB maximum
- Business plan: 200 MB maximum
- Enterprise plan: 500 MB default
No Nginx or PHP-FPM setting can override Cloudflare’s edge 413 error. For large media, backups, or video uploads over 100 MB, bypass your PHP server entirely by generating a temporary pre-signed upload URL directly to Amazon S3 or Cloudflare R2:
use Illuminate\Support\Facades\Storage;
['url' => $uploadUrl, 'headers' => $headers] = Storage::disk('s3')
->temporaryUploadUrl('uploads/' . $filename, now()->addMinutes(15));
return response()->json([
'upload_url' => $uploadUrl,
'headers' => $headers,
]);
Alternatively, split large files into 10–20 MB chunks on the frontend (using Resumable.js, Uppy, or FilePond) and reassemble them in Laravel once the final chunk arrives.
Step 4: Check Laravel Validation Rules & Livewire Limits
If your request clears Nginx and PHP-FPM cleanly but still fails validation, check two Laravel-specific traps:
1. Laravel’s max Rule Uses Kilobytes, Not Megabytes
Writing max:50 limits uploads to 50 KB, not 50 MB. For a 50 MB file limit, multiply by 1024:
$request->validate([
'document' => ['required', 'file', 'mimes:pdf,zip', 'max:51200'], // 50 MB in KB
]);
2. Livewire Temporary File Uploads Default to 12 MB
If you use Laravel Livewire, publish the config (php artisan livewire:publish --config) and raise temporary_file_upload.rules in config/livewire.php:
'temporary_file_upload' => [
'disk' => null,
'rules' => ['required', 'file', 'max:51200'], // 50 MB in KB
'max_upload_time' => 5,
],
Clear your cached config after editing:
php artisan config:clear
php artisan config:cache
Frequently Asked Questions
Why did php -i show my new upload_max_filesize, but the browser still threw PostTooLargeException?
Running php -i in your terminal reads the CLI configuration (/etc/php/8.3/cli/php.ini), whereas web traffic is served by PHP-FPM (/etc/php/8.3/fpm/php.ini). Always edit the fpm/php.ini file (or /usr/local/etc/php/conf.d/) and restart the php8.3-fpm service.
Should I set client_max_body_size 0; in Nginx to disable the limit completely?
No. Setting client_max_body_size 0; disables request size checks entirely and exposes your server to simple disk-exhaustion and denial-of-service attacks. Set an explicit ceiling (such as 20M or 64M) globally, and only raise it inside a specific authenticated upload location block if needed.
What if $_FILES is empty when the upload is larger than post_max_size?
When an incoming request exceeds post_max_size, PHP silently discards both $_POST and $_FILES before executing your controller. Laravel’s built-in ValidatePostSize middleware inspects the raw Content-Length header against post_max_size so you get a clear PostTooLargeException (413) instead of mysterious missing-field validation errors.