Fix "HTTP Error 413 Request entity too large" problem
Published 5 years and 3 months ago, Categorized under: Code, Laravel, Linux, Nginx, PHP

Yesterday Jeff Madsen has sent a small Laravel tip which address how you can validate the post size for your uploaded data via Laravel middleware called VerifyPostSize.

Even though the solution is valid when you have no control over your server, but when you do, you should fix the base of the problem which is your web server (in my case its Nginx ).

Normal when you upload a file, the process looks like this :

  1. The user request the website
  2. The Server get the request first and forward it to PHP process.
  3. PHP process do the magic and response back to the Server.

And the data get back from 3 to 1 .

Now as I said the solution which was provided solve the problem at the PHP processes level not at the Web server, at the end the data need to go through the server first.

In nginx documentation we have a core module which control the size of the data which the client can send through the server, the default size is 1Mb, so to fix this problem you need to do the following:

  1. edit the file /etc/nginx/nginx.conf and make sure that  the value for client_max_body_size is big enough like:
client_max_body_size 200M;
  1. edit your php.ini file to make sure that you have setup the upload size and memory with big values like :
memory_limit = 128M
post_max_size = 100M
upload_max_filesize = 200M

lastly you need to restart nginx.

now just a small note, you will need to adjust the values based on your needs, there are no magic values which can solve your issue here.

If you find that this information is incorrect, please let me know via email z[At] I'll be happy to get more information about this topics from experienced people out there.

PS: as usual, since english is not my mother language, you might find some errors, so if you do let me know and I will be happy to fix it.

Share It via: