A quick article today about a common error people encounter with Nginx: the infamous 413 Request Entity Too Large HTTP error. I had the problem myself just yesterday on one of my newly configured servers, so I thought I'd make a blog post about it to make sure I don't forget this next time.
Error 413: what does it mean?
This error shows up when a visitor sends too much data in the HTTP request. This is normally caused by:
• an overy large file being uploaded by the visitor
• more generally too much POST data being sent by the client
The fix is as simple as setting a directive in your Nginx configuration, read on below.
The fix is as simple as setting a directive in your Nginx configuration, read on below.
How to fix it? client_max_body_size
To fix this, you need to increase the value of the client_max_body_size directive. This directive defines the maximum amount of data Nginx will accept in an HTTP request. By default this value is set to 1 megabyte, meaning if you attempt to upload a file larger than 1 megabyte you'll be getting an Error 413: Request entity too large page. You can insert this directive at three levels:
- In the http block: this will set the directive value for all server and locations in your configurationn
- In the server block: this will set the directive value for all locations of one particular server
- In the location block: this will set the directive value for one specific location in a particular server
In this example I'm going to insert it in my http block and set it to 500 megabytes:
http {
client_max_body_size 500M; # allows file uploads up to 500 megabytes
[...]
}
Anything else I need to do?
After changing your server configuration, don't forget to reload Nginx for the configuration to apply. There are also some configuration settings you need to change if you are using PHP. If you aren't, just ignore the rest of this post. You'll need to open your php.ini configuration file and look for two values:
- upload_max_filesize: Maximum allowed size for uploaded files (default: 2 megabytes). You need to increase this value if you expect files over 2 megabytes in size.
- post_max_size: Maximum size of POST data that PHP will accept (default: 8 megabytes). Files are sent via POST data, so you need to increase this value if you are expecting files over 8 megabytes.
After making the changes, don't forget to reload PHP-FPM as well so that the changes are applied correctly. That's all! You shouldn't be getting any more upload errors after that.
No comments:
Post a Comment