The Fix
Add one line to the server block of the site, sized for the largest upload you expect:
server {
server_name example.com;
client_max_body_size 64m;
# the rest of the block
}
sudo nginx -t
sudo systemctl reload nginx
The server block is in /etc/nginx/sites-available/ on Debian and Ubuntu and in /etc/nginx/conf.d/ on AlmaLinux and Rocky Linux. Reload rather than restart: if the test failed, a reload leaves the running server untouched, while a restart with a broken file takes the site down.
What the Limit Does
client_max_body_size caps the size of the request body, which is where uploaded files and posted forms travel. The default is 1m, one megabyte. When a request body is larger, Nginx refuses it with 413 and the application never sees it. The Nginx documentation warns that browsers cannot always display this error correctly, so an upload may fail with a generic message, but the access log records the 413:
sudo grep '" 413 ' /var/log/nginx/access.log | tail
Where to Put It
- http block: in
/etc/nginx/nginx.conf, the value for every site. - server block: one site.
- location block: one path, such as an upload endpoint.
The most specific value wins, so a small value left in a location overrides a large one in the server block. Find every existing setting:
sudo nginx -T | grep -n client_max_body_size
Raising the limit only where uploads arrive keeps the small default for everything else:
location /upload/ {
client_max_body_size 200m;
proxy_pass http://127.0.0.1:3000;
}
Units are k, m, and g. A value of 0 turns the check off; avoid that on a public site.
PHP Limits Behind Nginx
With PHP-FPM, PHP has two limits of its own:
- upload_max_filesize: the largest single file; the default is 2M.
- post_max_size: the whole request body; the default is 8M. Keep it larger than the file limit.
upload_max_filesize = 64M
post_max_size = 72M
Set them in the php.ini that PHP-FPM reads: /etc/php/8.3/fpm/php.ini on Debian and Ubuntu (8.3 on Ubuntu 24.04, 8.4 on Debian 13, 8.5 on Ubuntu 26.04), and /etc/php.ini on AlmaLinux and Rocky Linux. Then restart PHP-FPM: sudo systemctl restart php8.3-fpm, or php-fpm on the Red Hat family. The command-line PHP reads a different file, so php -i may show other values; see how to install PHP on Ubuntu.
A request over the PHP limits does not cause a 413. A file over upload_max_filesize is dropped with an upload error, and a body over post_max_size leaves the posted fields and files empty, so the application reports an empty upload or a failed form.
Slow and Large Uploads
A big file over a slow connection can also hit time limits. client_body_timeout in Nginx, 60 seconds by default, applies between two reads of the body, so a steady upload is not cut off. In PHP, max_input_time limits how long a script may spend parsing input; its default, -1, means the max_execution_time value is used. Very large transfers are often better done over SFTP than through a web form.
Every Layer Has a Limit
If Nginx passes requests to an application, the application may have its own body limit, such as the body parser of a Node.js framework. A CDN or load balancer in front can set a limit too. Raise the value on each layer the upload passes through, from the outside in. Other request-size errors are explained in 431 Request Header Fields Too Large.
Confirm the Fix
Upload a file a little below the new limit through the site's own form, then look at the end of the access log:
sudo tail -n 5 /var/log/nginx/access.log
The upload request should now show 200, or the redirect the form sends after success, instead of 413. A file above the limit should still get 413, which shows that the limit is in force. If the request passes Nginx but the file never arrives, the PHP or application limit is the one left to raise.