Learn how to fix the nginx 413 Request Entity Too Large error by correctly configuring client_max_body_size at the right level and matching PHP-FPM upload limits.
Prerequisites
- A server running nginx with sudo access
- An existing nginx virtual host or server block
- PHP-FPM installed if the application uses PHP for uploads
What this solves
When nginx rejects a file upload with a 413 error, the log shows a line like "client intended to send too large body". This happens because the request body exceeds the client_max_body_size directive, which defaults to 1MB. This tutorial shows you how to locate the right config block, raise the limit safely, and keep PHP-FPM in sync so uploads actually succeed end to end.
Step-by-step configuration
Confirm the error in the logs
Before changing anything, confirm this is really a client_max_body_size issue and not a PHP or application-level limit. Check the nginx error log for the exact message.
sudo tail -n 50 /var/log/nginx/error.logYou are looking for a line similar to this:
2024/05/14 10:22:31 [error] 1234#1234: *567 client intended to send too large body: 15728640 bytes, client: 203.0.113.10, server: example.com, request: "POST /upload HTTP/1.1", host: "example.com"Find the active nginx config files
nginx loads a main config and then includes server blocks from sites-enabled or conf.d, depending on distro. Find which files are actually in use before editing anything.
sudo nginx -T | grep -n "client_max_body_size\|server_name\|include"If you installed nginx with virtual hosts already, you may have a dedicated server block file. If you have not set that up yet, see Install and configure Nginx virtual hosts for beginners first.
Understand where client_max_body_size can live
The directive is valid in the http, server, and location blocks. nginx uses the most specific match that applies to a request, so a location-level setting overrides a server-level one, which overrides the http-level default.
| Level | Scope | Typical use |
|---|---|---|
| http | All virtual hosts on this server | Set a sane global default, e.g. 10m |
| server | One domain or virtual host | Override for a specific site that needs larger uploads |
| location | One path, e.g. /upload | Allow large files only on the upload endpoint, keep the rest restricted |
Set the limit at the http level
This is the safest place to start if every site on the server needs a slightly higher default. Edit the main nginx config.
http {
client_max_body_size 10m;
...
}Override for a specific virtual host
If only one site needs larger uploads, for example a WordPress media library or a document management app, set it inside that server block instead of raising the global default.
server {
listen 443 ssl;
server_name example.com;
client_max_body_size 50m;
root /var/www/example.com;
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
}
}If you are running PHP with nginx for the first time, see Install and configure Nginx with PHP 8.4 for beginners.
Scope the limit to a single upload endpoint
The most precise fix is to only raise the limit for the path that actually accepts large files, keeping the rest of the site protected against oversized request abuse.
server {
listen 443 ssl;
server_name example.com;
location /upload {
client_max_body_size 100m;
proxy_pass http://127.0.0.1:8080;
}
location / {
client_max_body_size 10m;
try_files $uri $uri/ =404;
}
}Match PHP-FPM upload_max_filesize and post_max_size
If your app runs on PHP, nginx is only the first gate. PHP itself enforces its own limits, and these must be equal to or larger than the nginx value or uploads will still fail silently or return a PHP-level error instead of a 413.
php --ini | grep "Loaded Configuration"upload_max_filesize = 50M
post_max_size = 55M
memory_limit = 128Mpost_max_size should be slightly larger than upload_max_filesize since it covers the entire POST body, not just the file. If you are configuring PHP-FPM behind nginx for the first time, see Configure PHP-FPM with NGINX reverse proxy and SSL certificates.
Restart PHP-FPM to apply the ini changes
Unlike nginx, PHP-FPM needs a full restart, not a reload, for php.ini changes to take effect.
sudo systemctl restart php8.4-fpmsudo systemctl restart php-fpmTest the config and reload nginx safely
Always validate the config syntax before reloading. A reload with a broken config will leave the old config running but can mask errors until the next restart.
sudo nginx -tIf the test reports syntax is ok, reload rather than restart. A reload applies new config without dropping active connections.
sudo systemctl reload nginxVerify your setup
Create a dummy file close to your new limit and upload it with curl to confirm the server accepts it.
fallocate -l 20M /tmp/testupload.bin
curl -i -X POST -F "file=@/tmp/testupload.bin" https://example.com/uploadA successful response should return your application's normal 200 or 201 status, not a 413. You can also confirm the directive took effect directly.
sudo nginx -T | grep client_max_body_sizeThen test a file larger than your configured limit to confirm nginx still rejects oversized requests as expected.
fallocate -l 120M /tmp/toolarge.bin
curl -i -X POST -F "file=@/tmp/toolarge.bin" https://example.com/uploadCommon issues
| Symptom | Cause | Fix |
|---|---|---|
| 413 persists after editing nginx.conf | A more specific server or location block still has the old or default value | Run sudo nginx -T | grep client_max_body_size to see every occurrence and confirm which one applies |
| 413 persists after reload | You ran nginx -s reload but the config file edited was not the one actually included | Verify with sudo nginx -T which files are included and edit the correct one |
| Upload fails with a PHP error instead of 413 | nginx limit was raised but PHP-FPM upload_max_filesize or post_max_size is still too low | Raise both values in php.ini and restart php-fpm |
| Large uploads time out instead of failing with 413 | client_body_timeout or proxy read timeout is too short for slow connections | Increase client_body_timeout and proxy_read_timeout alongside client_max_body_size |
| Config test passes but site still serves old behavior | Reload was not applied, or a separate nginx instance or container is serving traffic | Run sudo systemctl status nginx and confirm only one nginx process tree is active |
| Permission denied writing uploaded files | Upload directory is not owned by the web server or PHP-FPM user | Run sudo chown -R www-data:www-data /var/www/example.com/uploads and set directories to 755, files to 644. Never use chmod 777 |
ps aux | grep php-fpm or check your nginx worker_processes user, then use chown and minimal permissions like 755 for directories and 644 for files instead.Next steps
- Install and configure Nginx with PHP 8.4 for beginners
- Configure PHP-FPM with NGINX reverse proxy and SSL certificates
- Fix nginx worker_connections are not enough warning
- Configure NGINX rate limiting and DDoS protection with advanced security rules
- Optimize NGINX performance for high-traffic websites