If you are seeing a 413 Request Entity Too Large error from Nginx, it means the server is rejecting a request because the body (the data you are sending) exceeds the client_max_body_size limit. The fix is to increase this limit in your Nginx configuration, typically in the http, server, or location block, and then reload Nginx.
What Causes the 413 Error in Nginx?
Nginx, by default, sets client_max_body_size to 1 megabyte. This is a deliberate safety measure to prevent clients from overwhelming the server with huge uploads. However, many modern web applications need to accept larger payloads—for example, uploading a video file, a high-resolution image, a database backup, or a JSON payload with many fields. When a request’s Content-Length header exceeds this limit, Nginx responds with 413 Request Entity Too Large before the application even sees the request.
The error is often confused with PHP or application-level upload limits (like upload_max_filesize in PHP), but the Nginx 413 occurs at the web-server layer. You might see it when using a CMS like WordPress, a file-sharing script, or a custom API endpoint that accepts file uploads.
How to Fix the 413 Error (Step-by-Step)
The solution is to edit your Nginx configuration file. The exact file depends on your setup, but the steps are generally the same.
- Locate the Nginx configuration file. Common paths include
/etc/nginx/nginx.conf(the main file) and/etc/nginx/sites-available/your-site(for virtual hosts). On some systems, it may be/etc/nginx/conf.d/default.conf. - Open the file with a text editor (e.g.,
sudo nano /etc/nginx/nginx.conf). - Add or modify the
client_max_body_sizedirective. You can place it in thehttpblock (applies to all servers), in a specificserverblock, or in alocationblock for a specific URL. For example, to allow 20 MB uploads, add:client_max_body_size 20m; - Test the configuration with
sudo nginx -t. This checks for syntax errors. - Reload Nginx to apply the changes:
sudo systemctl reload nginx(orsudo service nginx reload).
If you are using a reverse proxy setup (e.g., Nginx in front of Node.js or Apache), you may also need to check the proxy settings, but the client_max_body_size directive is the primary fix.
Where to Place the Directive: http, server, or location
The client_max_body_size directive is valid in three contexts. Choosing the right one depends on how broad you want the change to be.
- http block – sets the limit for all virtual hosts on the server. Good if you want a global default.
- server block – sets the limit for a specific domain or site. This is the most common choice.
- location block – sets the limit only for a specific URL path, like
/upload. Useful if you only want to allow large uploads on certain endpoints.
If you set the directive in multiple places, the most specific one wins. For example, a location block overrides a server block, which overrides the http block.
Common Mistakes and Misconceptions
One frequent mistake is forgetting to reload Nginx after editing the configuration. The change will not take effect until you do.
Another misconception is that the 413 error is caused by PHP’s upload_max_filesize or post_max_size. Those are separate limits. If you have increased Nginx’s limit but still get a 413, it might be because the request is being proxied to another server (like Apache or Node.js) that has its own limit. In that case, you need to adjust the upstream server’s settings as well.
Also, note that the client_max_body_size value can be specified in kilobytes (k) or megabytes (m). The default is 1m. If you set it to 0, it disables the limit entirely, which is not recommended for security reasons.
How to Choose the Right Size Limit
There is no one-size-fits-all value. The right limit depends on your application’s needs and your server’s resources.
- If you run a blog with occasional image uploads,
10mmight be plenty. - If you run a video-sharing platform, you might need
100mor even1g. - Consider the memory and disk space on your server. A very large limit can allow a malicious user to fill your disk with temporary files.
A good practice is to set the limit just above the largest legitimate file you expect. For example, if your max upload is 20 MB, set client_max_body_size 25m; to allow for overhead.
What If the Error Persists After Increasing the Limit?
If you have increased the limit and reloaded Nginx, but the 413 error persists, check the following:
- Is there a proxy? If Nginx is proxying requests to another server (e.g., Apache, Node.js, Gunicorn), that server may have its own upload limits. For example, Apache has
LimitRequestBody, and PHP haspost_max_size. - Is the request being handled by a CDN or load balancer? Some CDNs have their own request size limits. Check your CDN’s settings.
- Is the client sending a
Content-Lengthheader? If the request uses chunked transfer encoding, Nginx may still apply the limit based on the total size, but some clients might not send the header correctly. - Are there multiple Nginx config files? On some systems, you might have a main
nginx.confand separate files inconf.dorsites-enabled. Make sure you are editing the correct file that applies to your site.
If you are using a control panel like cPanel or Plesk, there may be a GUI setting for this, but you can also edit the configuration files directly.
Security Considerations and Best Practices
Increasing client_max_body_size is generally safe, but it does open the door for larger requests. This can be abused to exhaust server memory or disk space. To mitigate this, consider the following:
- Set a reasonable limit—don’t just set it to
0(unlimited) unless you have a specific reason. - Use rate limiting or request size monitoring to detect abnormal traffic.
- Ensure your application validates file types and sizes before processing.
- If you are behind a reverse proxy, make sure the proxy’s timeout settings are long enough for large uploads.
Also, remember that the client_max_body_size directive only affects the request body, not headers. If you have a very long URL or many custom headers, you might hit a different limit (large_client_header_buffers).
Frequently Asked Questions
What is the default Nginx client_max_body_size?
The default is 1 megabyte (1m). This is why you often see the 413 error when trying to upload files larger than 1 MB.
Can I set client_max_body_size to 0 to disable the limit?
Yes, setting client_max_body_size 0; disables the limit entirely. However, this is not recommended because it allows unlimited request sizes, which could be exploited to crash your server. It’s better to set a specific large value if needed.
Why does my PHP upload still fail after increasing Nginx’s limit?
PHP has its own upload limits: upload_max_filesize and post_max_size. These are usually set in the php.ini file. You need to increase those as well. Also, if you are using PHP-FPM, you may need to adjust the client_max_body_size in the Nginx config that proxies to PHP-FPM.
Does the 413 error appear in the browser or in the server logs?
It appears in the browser as a 413 Request Entity Too Large response. In the Nginx error log (usually /var/log/nginx/error.log), you will see a line like client intended to send too large body.
What is the difference between 413 and 400 errors?
A 400 Bad Request is a general error indicating the server could not understand the request. A 413 specifically means the request body is too large. If you see a 400 after increasing the limit, it might be due to a different issue, such as malformed request headers.
Related
Formula used
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t