Fix nginx client intended to send too large body error

Beginner 15 min Dec 01, 2026 18 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 Debian 11 AlmaLinux 9 Rocky Linux 9 Fedora 41

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.log

You 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.

LevelScopeTypical use
httpAll virtual hosts on this serverSet a sane global default, e.g. 10m
serverOne domain or virtual hostOverride for a specific site that needs larger uploads
locationOne path, e.g. /uploadAllow 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;
    }
}
Note: A value of 0 disables the check entirely. Avoid this in production, it removes an important safeguard against resource exhaustion from oversized requests.

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 = 128M

post_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-fpm
sudo systemctl restart php-fpm

Test 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 -t

If the test reports syntax is ok, reload rather than restart. A reload applies new config without dropping active connections.

sudo systemctl reload nginx

Verify 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/upload

A 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_size

Then 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/upload

Common issues

SymptomCauseFix
413 persists after editing nginx.confA more specific server or location block still has the old or default valueRun sudo nginx -T | grep client_max_body_size to see every occurrence and confirm which one applies
413 persists after reloadYou ran nginx -s reload but the config file edited was not the one actually includedVerify with sudo nginx -T which files are included and edit the correct one
Upload fails with a PHP error instead of 413nginx limit was raised but PHP-FPM upload_max_filesize or post_max_size is still too lowRaise both values in php.ini and restart php-fpm
Large uploads time out instead of failing with 413client_body_timeout or proxy read timeout is too short for slow connectionsIncrease client_body_timeout and proxy_read_timeout alongside client_max_body_size
Config test passes but site still serves old behaviorReload was not applied, or a separate nginx instance or container is serving trafficRun sudo systemctl status nginx and confirm only one nginx process tree is active
Permission denied writing uploaded filesUpload directory is not owned by the web server or PHP-FPM userRun sudo chown -R www-data:www-data /var/www/example.com/uploads and set directories to 755, files to 644. Never use chmod 777
Never use chmod 777. It gives every user on the system full read, write and execute access to your upload directory. If uploads fail due to permissions, find the correct owner with 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

Running this in production?

Want this handled for you? This works for a single server. When you run multiple environments or need uploads available 24/7 without silent 413s breaking a customer-facing form, keeping it healthy is a different job. See how we run infrastructure like this for European teams.
#nginx #413 error #client_max_body_size #php-fpm #file upload #web server configuration

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. Fully managed, with one fixed contact who knows your setup.

You get one fixed contact who knows your setup

Rotterdam 19:48 · reachable in a message, no ticket form

Learn more: managed cloud infrastructure