413 Error Redirect Not Working In Flask

My Flask app works fine on Linode with the exception of one thing: Handling of large files. I have in my configurations MAX_CONTENT_LENGTH set to, say 3 MB (3 * 1024 * 1024):

MAX_CONTENT_LENGTH = config.get('MAX_CONTENT_LENGTH') * 1024 * 1024

Where MAX_CONTENT_LENGTH in /etc/flask_app_config.json is:

{
    "MAX_CONTENT_LENGTH": 3
}

The app object (app = Flask(__name__)) is able to read this.

I have a simple way of handling the 413 error, where a user is redirected to a pre-created "File Too Large Error" template.

@bp.app_errorhandler(413)
def too_large_error(error):
    return render_template(
        'errors/413.html', title='Request Entity Too Large'), 413

Locally, everything works. I am not sure why on my Linode Ubuntu server this is not working. Instead, I get the plain white and unpleasant 413 Request Entity Too Large server page.

What could I be missing out on?

I haven't attempted using client_max_body_size 5M;, for example, in /etc/nginx/nginx.conf because I have already defined my configurations.

1 Reply

This has been a difficult question to answer because everything I've come up with seems to point to the two things you've said you didn't try, which I'm sure you're not trying for good reason.

That said, I'd like to share the information anyway. You can read this NGINX discussion for more about the error and the two solutions you mentioned, but there is also a patch mentioned in an update from just a few months ago.

I've never used Flask personally, but I tried to find out more about how how it passes that error response to NGINX to see where a change might need to be made. In the research I've done, I've found vague mentions of updating the versions of python, NGINX, Werkzeug, or Flask. I've found a few issues relating to something failing to read json correctly. I've found a few mentions of directories not having proper permissions to actually share configurations with an application and one issue about needing to tell Werkzeug that it's behind a proxy. All of these things were ultimately not directly related to the issue you mentioned, so I don't think the resources would be helpful, but I wanted to at least pass along some general ideas in case they spark something you haven't considered yet.

While the most straightforward solution I've seen is a custom error html file, this Flask Documentation for Error Handling is probably the most complete resource about this setting. There may be some help there and includes various situations that require different configurations and debugging help.

Hopefully someone who is more familiar with Flask comes to our rescue here, but I did wanted to try to help, even if I could only offer vague ideas at this point.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct