How to solve Nginx 413 Request Entity Too Large error?
Set Nginx file upload limit in correct location; Verify client_max_body_size, config test and application limits together.
Nginx uploading files to application behind413 Request Entity Too Largeerror, the request body may exceed the limit a layer accepts. Same situation with new HTTP naming413 Content Too LargeIt can also be seen as.
Basic setting in Nginx sourced caseclient_max_body_sizeis the value. However, if the application uses another proxy or CDN with a lower limit, simply changing the Nginx setting is not enough. First determine which layer produces the 413 response.
1. Verify that the response is from Nginx
Examine the Network log of the request and the corresponding Nginx error log at the same time interval.client intended to send too large bodyThe similar message indicates that Nginx is enforcing the body limit. The Server header in the response alone is not conclusive evidence; proxy headers can be changed.
The CDN, ingress or a different reverse proxy in between may also reject the request before it reaches the application. If there is no record in the Nginx log, follow the request path instead of editing the config of the wrong server. If there is 413 in the application log, investigate its upload and parser limits.
2. Set the limit in the correct server or location
client_max_body_sizehttp can be used in the context of server or location. default value1mis. The following example sets a body limit of around 25 MB for the upload endpoint:
server {
listen 80;
server_name app.example.com;
location /api/upload {
client_max_body_size 25m;
proxy_pass http://127.0.0.1:3000;
}
}
This is a configuration piece; Adapt it to the correct location in your own existing server block.proxy_passThere is no trailing slash in the example; Preserve the URI forwarding behavior to the application along with existing routes. On the system using HTTPS, edit the 443 server block that processes the request.
This limit covers the entire HTTP request body, not just the size of the file. Multipart headers and other fields are also included in the body. If your application accepts 20 MB files, it would be more understandable to keep the Nginx body limit slightly higher than the file limit and check the actual limit per file in the application.
3. Check active configuration and syntax
sudo nginx -T
sudo nginx -t
nginx -TShows the active configuration with included files. The output may contain private information; Do not share publicly. If there is a lower limit in a more specific location, it can override the value in the parent context. Check the block that the request matches, not just the file you're editing.
nginx -tAfter success, on Linux server using systemd:
sudo systemctl reload nginx
The reinstallation method varies on different installations; Nginx's heartbeat method or your container service's normal deployment flow can be used. Do not force a reload when there is a syntax error. Check the error log after reload.
4. Persist the setting in Docker
If you are using Nginx container, writing the setting only inside the running container may be lost when recreated. Mount the versioned config on the host with bind mount or add it to the image. Then test config within the relevant container and reload with your deployment flow.
In installations using Kubernetes ingress, the controller's own configuration options apply. Do not copy the plain Nginx config sample to all ingress products in the same format; Check the body limit setting documented by the controller you are using.
5. Match app and proxy upper limits
Upload and POST limits in PHP, request body and multipart limits in ASP.NET Core, body parser or file upload middleware in Express are separate layers. If the CDN has a fixed plan limit, Nginx cannot handle this cap. Determine the accepted size all the way.
client_body_buffer_sizewith memory buffer,proxy_read_timeoutIt is about timeout; these do not replace the body size limit.client_max_body_size 0Turns off this check of Nginx. A moderate limit that meets the need is a better start than unlimited loading.
Test the solution on both sides of the border
Try two files above and below the accepted size, with the actual upload flow including authentication. The small file should be successful; The clear limit message of the application should appear in the large file. Frontend authentication improves user experience, it does not replace the server limit.
Short guide
Common mistake
Changing the wrong server/location block or trying to solve the 413 size error by increasing the timeout setting.