Report this

What is the reason for this report?

flask app behind load balancer - 413 Payload too large error

Posted on September 24, 2023

My app configuration sets the max size of a file upload to 20 MB ( MAX_CONTENT_LENGTH = 20 * 1024 * 1024). The client includes the Content-Type’: ‘multipart/form-data’.

I’m at a loss to understand what is preventing my ability to have clients upload files greater than 1 MB (I can upload 0.8 MB).

Thank you for any confirmation of what to expect so that I can isolate the issue.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

What I could suggest is as a means of isolating the issue, if possible, try uploading directly to the Flask application (bypassing the Load Balancer). If it works, then the issue is likely with the Load Balancer or the network in between. If it doesn’t, the issue is within the Flask app or its immediate hosting environment.

The “413 Payload Too Large” error can be generated by several components in your infrastructure.

If this does not work even without the load balancer, what I could suggest is:

  1. Double-check your Flask app’s configuration to make sure MAX_CONTENT_LENGTH is correctly set to 20 * 1024 * 1024.

  2. Web Server Configuration:

    • If you’re using a web server like Nginx or Apache to serve your Flask app, they have their own client body size limits.

    • For Nginx: Check your Nginx configuration for the client_max_body_size directive. By default, it’s set to 1MB. To set it to 20MB, you’d add:

      client_max_body_size 20M;
      

      to your server or location block in the Nginx configuration.

    • For Apache: If you’re using mod_proxy, you might need to tweak LimitRequestBody. The default is unlimited, but if it’s set, it could be causing the issue.

  3. Error Logs:

    • Check the error logs of your Flask application, as well as any web server (Nginx, Apache) logs. They can provide more detailed information on why a request might be rejected.
  4. Middleware or Proxy:

    • Ensure there isn’t middleware in your Flask app or another proxy layer that’s causing the size restriction.

Let me know how it goes!

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.