Fixing 413 Upload Errors in Laravel with Nginx (5 Layers)

event November 25, 2026
update Updated: November 24, 2026
timer 12 min
Laravel 413 upload error cover: browser-to-storage chain with the Nginx bottleneck and a five-cap checklist
favorite 0

If you only want a one-line answer: if you landed on this page searching for Laravel Nginx 413 upload or "Laravel 413 upload error," the 413 Request Entity Too Large error means Nginx rejected the request before it reached PHP-FPM because the body was too big. The simplest fix is to set client_max_body_size 100m; in Nginx, but if you are behind a CDN or reverse proxy, or if files are larger than 100 MB, that alone is not enough. In practice you need to align 5 layers: Nginx, CDN/Proxy, PHP-FPM, Laravel Validation, and Storage. The rest of the article unpacks all five with numbers, tests, and an anti-pattern.

Prerequisites: What You Should Know Before Reading

This article is for someone who:

  • Works with Laravel 10+ and knows FormRequest and the max:KB validation rule.
  • Has set up Nginx with PHP-FPM in production at least once.
  • Is hitting 413 (Request Entity Too Large) or "the file does not upload," without a clear browser-side error message.

If you run Nginx on Windows or Apache, this article does not apply directly. For an internal app, read the official Laravel Validation docs.

Anatomy: Where Does 413 Come From?

If you want to know exactly when and where 413 request entity too large happens, according to the official Nginx client_max_body_size documentation:

Default: client_max_body_size 1m; Context: http, server, location Sets the maximum allowed size of the client request body. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error.

The second golden note: browsers cannot correctly display this error. That means the user sees a blank page or a generic error in the browser, not the literal 413 message. Many people think the browser or frontend JavaScript is broken, when Nginx actually cut the connection at layer 7 before the request ever reached Laravel.

Upload request path:

Browser (multipart/form-data)
    ↓
[CDN / Reverse Proxy] (if you have one)
    ↓
Nginx (client_max_body_size check)
    ↓
PHP-FPM (upload_max_filesize, post_max_size)
    ↓
Laravel (validation max:KB)
    ↓
Storage (S3 / local disk)

If any layer has a smaller cap than your file, that layer returns 413 (or its equivalent). In the table below, you see every layer with its directive/option and default value:

Layer Directive / Option Default Equivalent Error
Nginx client_max_body_size 1m 413 Request Entity Too Large
PHP-FPM upload_max_filesize 2M $_FILES empty
PHP-FPM post_max_size 8M $_POST and $_FILES empty
PHP-FPM memory_limit 128M Fatal error / blank page
Laravel validation: max:2048 (KB) zero (unlimited) ValidationException
Laravel validation: file false (unlimited) ValidationException

Layer 1: Nginx - client_max_body_size and Friends

Set client_max_body_size nginx in nginx.conf or in your site's vhost. This directive can live in three contexts (http, server, location); if you put it inside the upload location, only that endpoint inherits it. Putting it at the server level means all endpoints inherit the nginx upload limit:

server {
    listen 80;
    server_name example.com;
    client_max_body_size 100m;        # ← maximum request body size

    # Tunings for large uploads
    client_body_buffer_size 128k;     # ← in-memory buffer before writing to disk
    client_body_timeout 60s;          # ← timeout while reading the body from the client
    client_header_timeout 60s;        # ← timeout for reading the header

    root /var/www/example.com/public;
    # ... rest of the config
}

Explanation of Each Directive:

  • client_max_body_size 100m - the maximum total request body size. If your file is 50 MB, this needs to be at least 52m (because form overhead exists). This can sit at http, server, or location. If you only need it for one upload endpoint, put it inside location /upload rather than globally.
  • client_body_buffer_size 128k - Nginx keeps this much in RAM before writing to a temp file. If the file is bigger, it spills to disk. Raise it for large files to reduce disk I/O.
  • client_body_timeout 60s - if the client fails to finish sending the body in this window, Nginx returns 408. For uploads over a slow connection, raise it up to 300 seconds.

Common Trap: Forgetting the Suffix

One of the most common mistakes - and one I made once in production - is forgetting the suffix:

# ❌ Wrong - 64 bytes, not 64 megabytes!
client_max_body_size 64;

# ✅ Correct - 64 megabytes
client_max_body_size 64m;

Nginx understands k, m, g suffixes. Without a suffix, the number is interpreted as bytes. If you set 64 and upload a 5 MB file, Nginx will definitely return 413 and you will not understand why.

Different Behavior Based on the Request Type

According to the official Nginx documentation, the behavior of client_max_body_size depends on whether the request carries Content-Length or uses Transfer-Encoding: chunked:

Request Type Nginx Behavior
Content-Length: 10485760 Nginx checks immediately and, if it is bigger, returns 413 before reading any body. Not a single byte of the request is accepted.
Transfer-Encoding: chunked Nginx checks progressively. If the total chunks exceed the limit, 413 is returned mid-upload.

If you see this in the Nginx log, it means chunked encoding was used:

client intended to send too large chunked body: 5242880+1****76 bytes

Risk-Free Test Before Reload:

sudo nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

If you see syntax is ok and test is successful, reload. If it errors out, never reload - fix it first.

sudo systemctl reload nginx
# or the classic:
sudo nginx -s reload

Layer 2: CDN / Reverse Proxy - Cloudflare and HAProxy

If you are behind Cloudflare, this layer is unforgettable. Cloudflare caps uploads at 100 MB by default (on Free) or 500 MB (on Pro/Business). If your file is larger, Cloudflare errors out before Nginx.

Settings in Cloudflare:

  1. Open the dashboard → Network → Upload Size.
  2. On the Free plan you cannot go higher than 100 MB. For larger files you must either buy Pro or bypass Cloudflare for the upload (DNS-only or gray cloud).
  3. If the upload targets a specific subdomain, gray-cloud that subdomain so the traffic goes directly.

If Nginx Sits Behind Another Nginx (Reverse Proxy):

In the upstream Nginx you also need to set client_max_body_size, otherwise the front Nginx will return 413:

# upstream (back Nginx)
location /upload {
    proxy_pass http://backend;
    proxy_read_timeout 300s;          # ← for large uploads
    proxy_send_timeout 300s;
    client_max_body_size 200m;        # ← also set in the back Nginx
}

According to the official Nginx documentation, this directive can be set in three contexts: http, server, location. If you are not placing it inside the upload location, set it at the server level.

Layer 3: PHP-FPM - The upload_max_filesize / post_max_size / memory_limit Trio

According to the official PHP ini.core documentation:

Generally speaking, memory_limit should be larger than post_max_size. ... If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty.

That means if post_max_size=8M but your file is 10 MB, $_FILES stays empty - Laravel never sees the file and validation throws an error.

The Exact Relationship:

memory_limit > post_max_size > upload_max_filesize

Not the other way around. Most tutorials get this wrong.

Settings in /etc/php/8.3/fpm/php.ini:

; Maximum size of an uploaded file (100 MB here)
upload_max_filesize = 100M

; Maximum total POST form size (must be larger than upload)
post_max_size = 120M

; Maximum PHP memory per request (must be larger than post_max_size)
memory_limit = 256M

; Maximum script execution time (raise for big files)
max_execution_time = 300

; Maximum time for parsing input (e.g. receiving and parsing the file)
max_input_time = 300

Reload PHP-FPM After the Change:

sudo systemctl reload php8.3-fpm
# or
sudo service php8.3-fpm reload

Verify With a Command:

php -i | grep -E "upload_max_filesize|post_max_size|memory_limit"
# upload_max_filesize => 100M => 100M
# post_max_size => 120M => 120M
# memory_limit => 256M => 256M

If it shows the old numbers, you forgot to reload FPM or edited the wrong php.ini. Use php --ini to find which file is loaded. Remember: the exact php upload_max_filesize post_max_size relationship is that post must be larger than upload (form overhead).

Layer 4: Laravel Validation - max:KB and File Rules

Once Nginx and PHP-FPM are correct, the file reaches Laravel. But Laravel has its own validation cap, and if you do not set it, everything passes (which is bad - a 1 GB upload would fill the disk).

In a FormRequest:

public function rules(): array
{
    return [
        'avatar' => ['required', 'file', 'max:5120', 'mimes:jpg,png,webp'],
        //                                        ↑
        //                                   5120 KB = 5 MB
        'video'  => ['required', 'file', 'max:51200'],  // 50 MB
        'pdf'    => ['required', 'file', 'mimes:pdf', 'max:10240'], // 10 MB
    ];
}

Custom Error Messages (UX Matters):

public function messages(): array
{
    return [
        'avatar.max' => 'Profile picture must not be larger than 5 MB.',
        'video.max'  => 'Video must not be larger than 50 MB.',
        'avatar.mimes' => 'Image format must be jpg, png, or webp.',
    ];
}

According to the official Laravel Validation docs, the max rule for files is in kilobytes, not megabytes. Common mistake: max:5, which is 5 kilobytes! The correct laravel validation max file size pattern is: 'avatar' => ['file', 'max:5120', 'mimes:jpg,png'].

Why Should Validation Be Tighter Than Nginx?

If only Nginx enforces a cap, the user sees a 413 message that the browser cannot display properly. If only Laravel enforces a cap, the file passes through Nginx and PHP, consumes RAM, and Laravel rejects it - wasting server resources. Best strategy: a tight Nginx cap (e.g. 100 MB) plus precise Laravel validation (e.g. 5 MB for avatars).

Layer 5: Storage - Disk Space and S3 Multipart

Once the file reaches Laravel, you have two options for storage: a local disk or S3.

Local Disk:

# Check disk space
df -h /var/www

# If full, the upload fails with a disk error

In one real experience with a captaincode client project, /var/www was on a 20 GB LVM and after two weeks of 50 MB video uploads it filled up; the app just showed a blank page with no specific error.

S3 Multipart Upload:

If a file is larger than 100 MB, Laravel alone cannot handle S3 multipart upload. You need league/flysystem-aws-s3-v3 with a partSize setting:

Storage::disk('s3')->put(
    'videos/large.mp4',
    file_get_contents($request->file('video')),
    ['partSize' => 50 * 1024 * 1024]  // 50 MB chunks
);

Or for very large files, upload straight from the frontend to S3 via a presigned URL (Laravel only signs the request). That is the production-grade pattern for files over 1 GB.

Choosing the Right Cap by File Type

This table is something no other competitor has in this shape. Instead of a random number, set the cap based on the file type, disk space, and real timeout:

File Type Suggested Nginx Cap Suggested PHP Cap Suggested Laravel Validation Reason
Profile picture 10m 12M max:5120 (5 MB) Pictures larger than 5 MB are usually user mistakes
Article image 20m 24M max:10240 (10 MB) DSLR photos can reach 8 MB
PDF file 30m 32M max:20480 (20 MB) A book PDF is usually 5-20 MB
Short video 200m 220M max:102400 (100 MB) A 2-minute MP4 video
Long video 2g 2G No validation, S3 presigned Big videos should go directly to S3
CSV / Excel 50m 60M max:25600 (25 MB) 1 million-row CSV is about 20 MB

The General Formula:

Nginx client_max_body_size ≈ validation_max × 1.05 (form overhead)
PHP upload_max_filesize     ≈ validation_max
PHP post_max_size          ≈ upload_max_filesize × 1.2
PHP memory_limit           ≈ post_max_size × 2
client_body_timeout        ≈ file size / upload speed + 30s margin

Safe Production Test Sequence

Do this sequence in exactly this order:

1. Syntax Test (No Reload):

sudo nginx -t

If it errors, do not reload. Fix it first.

2. Create Test Files Exactly at the Limit:

# 100 MB file with dd
dd if=/dev/zero of=/tmp/test_100mb.bin bs=1M count=100

# 100 MB and 1 byte (1 byte over the limit)
dd if=/dev/zero of=/tmp/test_100mb_plus1.bin bs=1M count=100
printf 'a' >> /tmp/test_100mb_plus1.bin

3. Reload Nginx and PHP-FPM:

sudo systemctl reload nginx
sudo systemctl reload php8.3-fpm

4. Upload Tests:

# Test with curl, upload endpoint
curl -X POST http://example.com/upload \
  -F "file=@/tmp/test_100mb.bin"

# Should return 200/422 (validation success) or 200 (success)

curl -X POST http://example.com/upload \
  -F "file=@/tmp/test_100mb_plus1.bin"

# Should return 413 or 422 (depending on configuration)

5. Inspect the Nginx Log:

sudo tail -f /var/log/nginx/error.log
# On a successful upload, you see no error
# On a 1-byte-over upload, you see this:
# client intended to send too large body: 100000001 bytes

If you see client intended to send too large body, the Nginx config works correctly and you only need to raise the cap or tighten validation.

Anti-Pattern: Setting a Very High Cap Without a Timeout

The most common mistake: setting client_max_body_size 1000m without adjusting the timeout. The result:

  1. The user starts the upload.
  2. Nginx reads the request body (for example, 20 minutes for a 1 GB file on a slow connection).
  3. If client_body_timeout 60s, Nginx returns 408 after 60 seconds.
  4. The user retries, a new connection opens, RAM is consumed.
  5. The server OOMs or the worker pool fills up.

The fix: raise both the cap and the timeout together, not just the cap:

client_max_body_size 500m;
client_body_buffer_size 1m;       # larger buffer
client_body_timeout 600s;         # 10 minutes for big files
client_header_timeout 60s;

Or even better: send files larger than 100 MB directly to S3 via a presigned URL so Nginx is not involved at all.

What to read next

If all five layers are aligned and uploads still fail, send nginx.conf and the nginx -T output and we will look at it together, or reach out through the contact form and the Captain Code web team will tune the server for you.

Frequently Asked Questions

Why does the browser not show the 413 message?

According to the official Nginx documentation: "browsers cannot correctly display this error". Nginx returns a short HTML response, but during an upload (while still in submitting state) the browser may ignore it or show only a blank page. Fix: also add validation on the frontend so it checks the file size before sending.

What should the relationship between upload_max_filesize and post_max_size be?

According to the official PHP documentation, post_max_size must be larger than upload_max_filesize (because post includes form fields, not just the file). So if the file is 100 MB, post_max_size=120M and upload_max_filesize=100M. And memory_limit must be larger than post_max_size.

How do I confirm Nginx actually applied the new client_max_body_size value?

sudo nginx -T 2>/dev/null | grep client_max_body_size
# client_max_body_size 100m;

If it shows the old number, you forgot to reload or edited the wrong file. If Nginx has multiple vhosts, make sure you put the directive inside the active site's server block, not an included block that gets overridden.

Can I just set client_max_body_size 0?

Yes, according to the official Nginx documentation, 0 means Nginx does not check. But then PHP-FPM and Laravel have to manage it alone - and if Laravel validation is weak, the server becomes a DoS target. Not recommended unless Laravel validation is extremely tight.

Cloudflare Free won't allow uploads over 100 MB; what do I do?

Two options: (1) Upgrade to Pro/Business, which raises the cap to 500 MB. (2) Gray-cloud (DNS-only) the upload subdomain so Cloudflare is bypassed and Nginx receives the request directly. Option 2 is more common and cheaper.

Can I configure this without access to php.ini on shared hosting?

Yes - with a .user.ini file at the project root (next to artisan). PHP-FPM reads this file per-directory (like .htaccess for Apache):

; /home/user/example.com/public_html/.user.ini
upload_max_filesize = 100M
post_max_size = 120M
memory_limit = 256M

According to the official PHP documentation, .user.ini requires cgi.fix_pathinfo=1 (default in most PHP-FPM setups) and is cached roughly every 5 minutes (controlled by user_ini.cache_ttl). For an instant test in production:

# On the shared hosting server
touch /home/user/example.com/public_html/.user.ini   # ← change the timestamp to invalidate the cache

If you have Apache behind Nginx, you can also use .htaccess with php_value - but on Nginx only .user.ini works.


If you align all five layers and set each cap from the type-based table, the large majority of Laravel Nginx 413 upload failures in production are solved by these few directives. If you still see the error after everything, it is probably at the browser level (CORS, cookies, CSRF) or storage level (S3 bucket policy), which deserve their own article.

Captain Code
verified
Author

Captain Code

View Profile arrow_forward

Passionate about coding and sharing knowledge. We strive to create the best content for you at Captain Code.

Was it useful? Like it 👋
Share:

FAQ

Why does the browser not show the 413 message?

According to the official Nginx documentation (http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size): "browsers cannot correctly display this error". Nginx returns a short HTML response, but during an upload (while still in submitting state) the browser may ignore it or show only a blank page. Fix: also add validation on the frontend so it checks the file size before sending.

What should the relationship between upload_max_filesize and post_max_size be?

According to the official PHP documentation (https://www.php.net/manual/en/ini.core.php), post_max_size must be larger than upload_max_filesize (because post includes form fields, not just the file). So if the file is 100 MB, post_max_size=120M and upload_max_filesize=100M. And memory_limit must be larger than post_max_size.

How do I confirm Nginx actually applied the new client_max_body_size value?

If it shows the old number, you forgot to reload or edited the wrong file. If Nginx has multiple vhosts, make sure you put the directive inside the active site's server block, not an included block that gets overridden.

Can I just set client_max_body_size 0?

Yes, according to the official Nginx documentation (http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size), 0 means Nginx does not check. But then PHP-FPM and Laravel have to manage it alone - and if Laravel validation is weak, the server becomes a DoS target. **Not recommended** unless Laravel validation is extremely tight.

Cloudflare Free won't allow uploads over 100 MB; what do I do?

Two options: (1) Upgrade to Pro/Business, which raises the cap to 500 MB. (2) Gray-cloud (DNS-only) the upload subdomain so Cloudflare is bypassed and Nginx receives the request directly. Option 2 is more common and cheaper.

Can I configure this without access to php.ini on shared hosting?

Yes - with a .user.ini file at the project root (next to artisan). PHP-FPM reads this file per-directory (like .htaccess for Apache): According to the official PHP documentation (https://www.php.net/manual/en/configuration.file.per-user.php), .user.ini requires cgi.fix_pathinfo=1 (default in most PHP-FPM setups) and is cached roughly every 5 minutes (controlled by user_ini.cache_ttl). For an instant test in production: If you have Apache behind Nginx, you can also use .htaccess with php_value - but on Nginx only .user.ini works. --- If you align all five layers and set each cap from the type-based table, the large majority of Laravel Nginx 413 upload failures in production are solved by these few directives. If you still see the error after everything, it is probably at the browser level (CORS, cookies, CSRF) or storage level (S3 bucket policy), which deserve their own article.

Captain Code

Want this built for your project?

Hire Captain Code to take the project from here, or try Nyx and debug it with the AI assistant.

forum

Comments

lock_person

Login to comment

To join the conversation, please log in to your account first.

login Login
chat_bubble_outline

No comments yet

Be the first to share your thoughts on this post!