DEV Community

Fajar Windhu Zulfikar
Fajar Windhu Zulfikar

Posted on • Originally published at fajarwz.com on

How to Fix 'Client Intended to Send Too Large Body' Nginx 413 Error

Nginx 413 illustration

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

Nginx 413

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
Enter fullscreen mode Exit fullscreen mode

Then insert the following rule

server {
    ...
    # set the maximum upload to 20MB
    client_max_body_size 20M;
    ...
}
Enter fullscreen mode Exit fullscreen mode

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;
    ...
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If it says "syntax is ok" then we are ready to restart our Nginx.

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

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

Reference

How to Limit File Upload Size in Nginx

Top comments (0)