Last reviewed: November 25, 2026
The last time I chased nginx 413 Request Entity Too Large in production, it was a "small" change. One of our Laravel modules let users attach scanned documents, and QA tested it with phone photos of around 800 KB. It worked. A week later, someone on the operations side uploaded a 4 MB PDF and the screen just said "Something went wrong". In the browser's Network tab, the request had failed with a plain HTML page: 413 Request Entity Too Large, signed by nginx. Laravel never saw it.
We raised client_max_body_size, reloaded nginx, and the next upload still failed, this time with no error at all, just an empty file field. That was PHP's own limit. On a different project, a Node.js import endpoint returned 413 as JSON, and that one had nothing to do with nginx. The status code is the same, but three different layers can send it.
This guide starts with finding who rejected the request, then fixes each layer. I reproduced every case below on a throwaway box with nginx 1.27.5, php-fpm 8.4 and Express 4.22. The outputs are copied from that test. Where something comes from documentation or framework source rather than my own test, I say so.
What the 413 error actually means
HTTP 413 means the server refused the request because the body is bigger than it is willing to accept. Newer HTTP specs call it "Content Too Large", but nginx still prints the older name, "Request Entity Too Large". In nginx, the limit is the client_max_body_size directive, and its default is 1m (1 MB). Any POST, PUT or PATCH with a body over 1 MB gets rejected unless you raise it.
The important part: nginx checks the Content-Length header before it passes anything to PHP, Node or your framework. That is why your app logs are empty. The request never got there.
A two-minute diagnosis: who sent the 413?
Reproduce it with curl so you can see the raw response. I made a 5 MB test file and posted it to an endpoint with no custom limit:
head -c 5M /dev/urandom > 5mb.bin
curl -si -F [email protected] http://127.0.0.1:8413/profile.phpHTTP/1.1 413 Request Entity Too Large
Server: nginx/1.27.5
Content-Type: text/html
Content-Length: 183
<html>
<head><title>413 Request Entity Too Large</title></head>
<body>
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx/1.27.5</center>
</body>
</html>How to read the response:
- HTML page with an nginx version at the bottom: nginx rejected it. Go to the nginx fix.
- JSON like
{"error":"request entity too large"}: your Node.js app (body-parser) rejected it. Go to the Express fix. - Laravel error page or JSON saying "The POST data is too large": Laravel's
PostTooLargeException. That is PHP'spost_max_size, not nginx. - Status 200, but the file is missing or
$_FILESis empty: PHP limits. No 413 at all, which is why this one wastes the most time.
Then check the nginx error log. nginx writes one line for every body it refuses, with the size the client tried to send and the server block that handled it:
sudo grep 'too large body' /var/log/nginx/error.log[error] 28#28: *3 client intended to send too large body: 5243091 bytes,
client: 172.17.0.1, server: _, request: "POST /profile.php HTTP/1.1", host: "127.0.0.1:8413"The server: field matters. If you have several server blocks, or a second nginx in front of the first, it tells you which one to edit. Finally, see what limit nginx is actually running with. nginx -T prints the full loaded config, including every included file:
sudo nginx -T 2>/dev/null | grep -n client_max_body_sizeOn my test box this printed nothing at first, which means the 1m default was in effect.
Fix 1: raise client_max_body_size in nginx
client_max_body_size can be set in the http, server or location context. A more specific block overrides a wider one. On Ubuntu, your site config is usually in /etc/nginx/sites-available/, so add it to the server block of the site that is failing:
server {
listen 80;
server_name app.example.com;
root /var/www/app/public;
client_max_body_size 64m;
# ... your existing location blocks
}Always test before you reload, so a typo does not take the site down:
sudo nginx -t && sudo systemctl reload nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulAfter that, the same 5 MB upload on my test box went through:
{"content_length":"5243091","post_max_size":"70M","upload_max_filesize":"64M","files":{"file":{"size":5242880,"error":0}}}
HTTP 200Better: raise it only on the upload route
I prefer to keep the small default for the whole site and allow big bodies only where uploads happen. A login form or a JSON API has no reason to accept 64 MB. This is the config I tested:
server {
listen 80;
root /var/www/html;
location = /upload.php {
client_max_body_size 64m;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}direct upload.php HTTP 200
direct profile.php HTTP 413For Laravel, where every request goes through index.php, there is a catch I confirmed on the test box. If the upload location just does try_files $uri /index.php?$query_string;, nginx redirects the request internally to the PHP location, and the limit of that location applies:
location ^~ /api/uploads/ {
client_max_body_size 64m;
try_files $uri /index.php?$query_string; # ends up in the 1m PHP location
}
# result: HTTP 413Pass the upload prefix to PHP-FPM directly from the same location instead:
location ^~ /api/uploads/ {
client_max_body_size 64m;
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;
}
# result: HTTP 200If that feels fiddly, setting the limit at server level is fine too. Just keep the real maximum enforced by Laravel validation.
If there is a second nginx in front
A common setup on our servers is a host nginx that terminates SSL and proxies to an app container that has its own nginx. Both check the body size. I tested a front proxy with no limit set, in front of the app server that already allowed 64m:
server {
listen 8080;
location / {
proxy_pass http://127.0.0.1:80;
}
}via front proxy HTTP 413
[error] client intended to send too large body: 5243091 bytes, client: 172.17.0.1, server: , request: "POST /upload.php HTTP/1.1"Note the empty server: in the log line. That is the front block (it has no server_name), not the app. After adding client_max_body_size 64m; to the front server block and reloading, the same request returned 200. The rule is simple: the smallest limit on the path wins. That includes load balancers, Cloudflare and any API gateway in front of you, which have their own upload limits; check their docs for the exact number on your plan.
Fix 2: raise PHP upload_max_filesize and post_max_size
This is the step people miss after fixing nginx. PHP has two limits of its own, and the official Docker image and most distro packages ship with upload_max_filesize = 2M and post_max_size = 8M. With nginx already at 64m, here is what my test endpoint returned:
== 5mb
{"content_length":"5243091","post_max_size":"8M","upload_max_filesize":"2M","files":{"file":{"size":0,"error":1}}}
HTTP 200
== 12mb
Warning: PHP Request Startup: POST Content-Length of 12583124 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
{"content_length":"12583124","post_max_size":"8M","upload_max_filesize":"2M","files":[]}
HTTP 200Two different failures, both with HTTP 200:
- File bigger than
upload_max_filesizebut the request underpost_max_size: the file arrives witherror: 1(UPLOAD_ERR_INI_SIZE) and size 0. Your other form fields are still there. - Whole request bigger than
post_max_size: PHP throws away the entire body.$_FILESand$_POSTare empty, so a form looks like it was submitted with nothing in it. In Laravel this also breaks the CSRF check, because the_tokenfield is gone too.
Set both, with post_max_size a bit larger than upload_max_filesize so other form fields fit. On Ubuntu with php-fpm, put them in a separate file so package upgrades do not overwrite them:
# /etc/php/8.4/fpm/conf.d/99-uploads.ini
upload_max_filesize = 64M
post_max_size = 70Msudo systemctl reload php8.4-fpmWatch out for one thing I hit on the test box. Right after adding the file, php -i showed the new values, but the upload still failed:
$ php -i | grep -E '^(upload_max_filesize|post_max_size)'
post_max_size => 70M => 70M
upload_max_filesize => 64M => 64M
$ curl -s -F [email protected] http://127.0.0.1:8413/upload.php
{"content_length":"5243091","post_max_size":"8M","upload_max_filesize":"2M","files":{"file":{"size":0,"error":1}}}php -i runs the CLI, which reads its own config and starts fresh every time. php-fpm keeps the values it loaded at startup until it is reloaded. After reloading php-fpm, both the 5 MB and 12 MB uploads returned the file with error: 0. On Ubuntu, the CLI and FPM also read different directories (/etc/php/8.4/cli/ and /etc/php/8.4/fpm/), so check the value from a web request, not from the terminal.
Laravel: PostTooLargeException
This part is from Laravel's source, not my box test. Laravel's ValidatePostSize middleware, which is in the default global middleware stack, compares the request's Content-Length with PHP's post_max_size. If the request is larger, it throws Illuminate\Http\Exceptions\PostTooLargeException with the message "The POST data is too large." and status 413. So a Laravel 413 means nginx let the request through and PHP's post_max_size is the limit to raise. The fix is the php-fpm change above, not nginx.
Keep a validation rule as well, so users get a clear message instead of a generic error, for example 'document' => ['required', 'file', 'max:61440'] (the max rule for files is in kilobytes). I covered the rest of a safe upload flow, including MIME checks and storage, in Laravel file upload security.
Fix 3: Node.js and Express return their own 413
If the 413 body is JSON, look at your Node app first. Express's built-in JSON parser (express.json(), which is body-parser) has a default limit of 100kb. I posted a 448 KB JSON import to a minimal Express 4.22 app:
const express = require('express');
const app = express();
app.use(express.json()); // default limit: 100kb
app.post('/api/import', (req, res) => res.json({ rows: req.body.rows.length }));
app.post('/api/import-big', express.json({ limit: '10mb' }),
(req, res) => res.json({ rows: req.body.rows.length }));/api/import {"error":"request entity too large"} HTTP 413
/api/import-big {"error":"request entity too large"} HTTP 413
# error handler log
entity.too.large request entity too large limit=102400 length=447790The second result is the trap. The route has its own 10mb parser, but the global app.use(express.json()) runs first and rejects the body at 100kb before the route parser ever runs. The fix is to register the big parser on that route before the global one:
const bigJson = express.json({ limit: '10mb' });
app.post('/api/import-big', bigJson, (req, res) => res.json({ rows: req.body.rows.length }));
app.use(express.json()); // 100kb for everything else
app.post('/api/import', (req, res) => res.json({ rows: req.body.rows.length }));/api/import {"error":"request entity too large"} HTTP 413
/api/import-big {"rows":5000} HTTP 200The same idea applies to express.urlencoded(), which has the same 100kb default. If nginx sits in front of the Node app, it still needs client_max_body_size raised for that path too.
For Next.js, written from the docs and not tested here: Server Actions have a default body limit of 1 MB, set with experimental.serverActions.bodySizeLimit in next.config.js, and the Pages Router API routes have api.bodyParser.sizeLimit, also 1 MB by default. For real file uploads I would rather send the file directly to object storage with a signed URL than push it through a Next.js function at all.
Fix 4: Kubernetes ingress-nginx
Written from the ingress-nginx documentation, not tested on the box. The controller sets client_max_body_size from the proxy-body-size value, which defaults to 1m. Set it per Ingress with an annotation:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: "64m"
spec:
ingressClassName: nginx
# ... rules unchangedOr set proxy-body-size: "64m" in the controller's ConfigMap to change it for every Ingress. Annotation values must be quoted strings.
Two notes. First, the community kubernetes/ingress-nginx project was retired in March 2026 and gets no more fixes, so if you are still on it, plan a migration. Second, F5's NGINX Ingress Controller (nginx/kubernetes-ingress) uses a different annotation, nginx.org/client-max-body-size: "64m". Using the wrong prefix does nothing and gives no error, which is a very quiet way to lose an afternoon. If the pod behind the Ingress runs its own nginx or Node app, its limits apply too.
Common mistakes
- Editing the wrong file or block. The limit in
nginx.conf'shttpblock is overridden by a smaller value in a server or location block. Runsudo nginx -T | grep -n client_max_body_sizeto see every value that is actually loaded. - Forgetting to reload. Saving the file changes nothing until
nginx -tpasses and nginx reloads. The same goes for php-fpm after an.inichange. - Editing a bind-mounted config with
sed -i. I did this on the test box with a single file mounted into the nginx container.sed -iwrites a new file, the container kept seeing the old one, and the upload still returned 413 after a reload. Mount the directory instead of the single file, or recreate the container. - Setting
client_max_body_size 0. It disables the check completely. Anyone can then stream gigabytes to any endpoint, including your login page. Use a real number, and only where it is needed. - Raising nginx and stopping there. PHP's
upload_max_filesizeandpost_max_size, Express's 100kb default and any proxy in front all still apply. - Chasing a CORS error. In a single-page app calling an API on another domain, the browser often reports the failed upload as a CORS error. That is because nginx's error page does not include headers added with
add_headerunless you usealways. On the test box, the 413 had noAccess-Control-Allow-Originheader until I addedalways; after that the header was there and the frontend could read the real 413. Check the Network tab status before touching CORS.
If the upload passes nginx but then takes too long, you will see a different error. Slow uploads to a slow backend are a common cause of nginx 504 Gateway Timeout, and a PHP-FPM worker that crashes on a big file usually shows up as a 502 Bad Gateway in nginx.
Questions I get asked
What is the default client_max_body_size in nginx?
1m (1 MB). Anything larger gets 413 unless you set a higher value in the http, server or location block that handles the request.
Why does my upload return 200 but the file is empty?
That is PHP, not nginx. The file is over upload_max_filesize (you get error code 1), or the whole request is over post_max_size (everything is empty). Raise both and reload php-fpm.
How big should the limit be?
Slightly above the largest file you actually accept, plus room for the other form fields. If your app accepts 50 MB documents, 64m in nginx and 64M/70M in PHP is reasonable. Enforce the real maximum in the app's validation, so users get a clear message.
About the author. Muhammad Wilayat Shah is a senior full-stack engineer and team lead based in Islamabad, with 13+ years shipping ERP and SaaS products with Laravel, Node.js, React and Next.js on Ubuntu servers behind nginx. He wrote this after fixing the same upload bug at three different layers in one week. More at wilayats.com/about.
Last reviewed: November 25, 2026




Comments
Join the discussion. Replies load on demand, up to 5 levels.
0 threads
No comments yet
Be the first to start the conversation.