flask app behind load balancer - 413 Payload too large error

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.

Submit an answer

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!

Sign In or Sign Up to Answer

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!



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel