Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel file upload returns 413 behind nginx

Error413 Request Entity Too Large

Short answer

A 413 means a limit refused the request body before Laravel could use it. nginx rejects bodies over client_max_body_size, which is 1m by default. PHP has its own two limits, upload_max_filesize and post_max_size. Raise the nginx value first, reload nginx, then keep post_max_size larger than upload_max_filesize.

  • Applies to nginx 1.26, PHP 8.3, Laravel 12, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send a test file just over 1 MB straight to the route and read the status line. A 413 with an nginx page means the nginx limit; a normal Laravel response (a validation message) means the PHP or Laravel limits.

    head -c 2000000 /dev/zero > /tmp/test-2mb.bin
    curl -i -F "file=@/tmp/test-2mb.bin" https://example.com/upload
  2. 2Match what you see, then jump to its fix

    1. 1The 413 is returned by nginx, so the request does not reach Laravel and nothing new appears in its log.nginx client_max_body_size is still at its 1m defaultFix
    2. 2You raised client_max_body_size, but the 413 is unchanged.The limit sits in a different block than the one that serves the routeFix
    3. 3nginx passes the request, but the upload still fails; the PHP defaults are 2M for a file and 8M for the whole POST.PHP's upload_max_filesize or post_max_size is lower than the fileFix

What does the error look like?

  • 413 Request Entity Too Large
  • 413 Content Too Large

Cause 1 of 3

nginx client_max_body_size is still at its 1m default

1Check

Search the active nginx configuration for the directive. If nothing sets it, the default of 1m applies to every request body.

grep -R "client_max_body_size" /etc/nginx/

2Fix

Set the directive to the largest upload you want to accept. It is allowed in the http, server and location contexts, so you can raise it only for the upload route.

server {
    server_name example.com;

    location /upload {
        client_max_body_size 50m;
        # ...usual fastcgi_pass to php-fpm...
    }
}

3Verify

Reload nginx and repeat the test upload. The status should change from 413 to whatever Laravel returns.

sudo nginx -s reload
curl -i -F "file=@/tmp/test-2mb.bin" https://example.com/upload

Confirmed by nginx.org

Did this fix it?

Cause 2 of 3

The limit sits in a different block than the one that serves the route

1Check

A value set in one server or location block does not reach another. Find every block that can answer this URL, including a second nginx or proxy in front of the app.

grep -R -n "client_max_body_size\|server_name\|location" /etc/nginx/sites-enabled/

2Fix

Put the directive in the block that actually handles the upload URL, or in the http context if every site may take large bodies. A value in a location block applies to that location only.

http {
    client_max_body_size 50m;
}

3Verify

Reload nginx; the master process applies the new configuration to fresh workers. Then upload again.

sudo nginx -s reload

Confirmed by nginx.org

Did this fix it?

Cause 3 of 3

PHP's upload_max_filesize or post_max_size is lower than the file

1Check

Print the limits from the PHP build that serves the site. FPM can use a different php.ini than the CLI, so check the FPM file.

php --ini
php -i | grep -E "upload_max_filesize|post_max_size|max_file_uploads"

2Fix

Raise both values in the php.ini that php-fpm loads. post_max_size has to be larger than upload_max_filesize, and the manual advises memory_limit above post_max_size. These settings cannot be changed with ini_set() at runtime.

; php.ini used by php-fpm
upload_max_filesize = 50M
post_max_size = 55M

3Verify

Restart php-fpm, then read the values back through a request or a phpinfo page that runs under FPM.

sudo systemctl restart php8.3-fpm

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Decide the real maximum upload size once and set the nginx and PHP values together, with post_max_size a little above the file limit.
  • Add the same size to Laravel's validation rule for the file so users get a clear message instead of a bare error page.
  • Keep a test file just over the limit in your deploy checklist and upload it after every server change.
  • For very large files, upload straight to object storage with a signed URL instead of through PHP.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

PHP stops the script after 30 seconds

Symfony\Component\ErrorHandler\Error\FatalError: Maximum execution time of 60 seconds exceeded

PHP ends a script that runs past max_execution_time (30 seconds by default for web requests).

5 causes, each with a fix

Back end

Why a Filament panel feels slow on a Laravel server

The panel is fast on a small demo and slow once there are many resources, pages and widgets

A slow Filament panel on a server is most often running without the production caches.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.