This can happen when a user upload a very large sized file to our app that served with Nginx. Maybe the user see the following error
And when we search for an error log for this in Nginx error log in - for example - for CentOS it is located at /var/log/nginx/error.log
, we found Client Intended to Send Too Large Body: xxxxxx bytes
error.
Let’s fix this by adding a rule in our Nginx config file. We need to add client_max_body_size
rule.
Setting for Spesific Site Only
If you want this rule for spesific site only, set this inside your spesific site config that located at /etc/nginx/sites-available/mywebsite.com.conf
For example We can use nano
to open the config file
sudo nano /etc/nginx/sites-available/mywebsite.com.conf
Then insert the following rule
server {
...
# set the maximum upload to 20MB
client_max_body_size 20M;
...
}
Setting for All Server Blocks
If we want the rule to affects all server blocks, set it inside /etc/nginx/nginx.conf
.
http {
...
# set the maximum upload to 20MB
client_max_body_size 20M;
...
}
Note: Setting size to 0 disables checking of client request body size.
Check Nginx Configuration And Restart
Next, check our configuration by running
sudo nginx -t
If it says “syntax is ok” then we are ready to restart our Nginx.
sudo systemctl restart nginx
Conclusion
That’s it, our app should be able to receive files with size under 20MB with this client_max_body_size
rule in Nginx.
Read Also
- Create Login With Google OAuth Using Laravel Socialite
- Create Reusable Query With Laravel Query Scope
- Laravel Rest API Authentication Using JWT Tutorial