2
votes

I have a Flask App running on remote ubuntu server with Nginx and Uwsgi. I can't upload files greater than ~200Kb.

To make my app I followed this tutorial: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04

at first I had error 413 which I fixed adding client_max_body_size 5M; in /etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        client_max_body_size 5M;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

Now I don't have any errors but still I'm not able to upload larger files. I also changed my app uwsgi configuration file:

[uwsgi]
module = wsgi:app

master = true
processes = 10

limit-post = 20000000
ignore-sigpipe=true
ignore-write-errors=true
disable-write-exception=true


socket = main.sock
chmod-socket = 660
vacuum = true

die-on-term = true

logto= /var/log/uwsgi/%n.log

What else can I do to fix it?

2

2 Answers

1
votes

Thank you Frank for your answer, has been some time since I opened this post. If I recall correctly the error was a bit more subtle: Flask doesn't have by default a "retry" logic builtin. This in combination with the cheapest server I could find for testing contribuite to the failure of some of the most expensive requests. Implementing a "retry" logic with the retry decorator solved my issue.

1
votes

It looks like you have set the variables correctly. Perhaps you have the config setting MAX_CONTENT_LENGTH set in your Flask app? It would look something like this:

app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # 50 MB

By default, Flask shouldn't complain with large requests, but if this variable is set then it will throw 413 errors when the request exceeds the specified value. Check here for more info.

Lastly, make sure you're reloading Nginx and your uWSGI service with these commands:

> sudo systemctl restart nginx.service
> sudo systemctl restart <yourservice>.service

The .service suffixes are optional.