Search code examples
socketsnginxflaskubuntu-18.04uwsgi

Nginx, uwsgi and flask issue with configuration


Up until now I had a Flask app running with uwsgi and nginx on Ubuntu 18.04 remote server. This app was served by a subdomain of my website listening to port 5002. I wanted to add a new Flask app to listen to port 5003, but I must have made some misconfiguration and then all hell broke loose. Now neither of the apps is working.

Server block of app1

server {
        client_header_buffer_size 64k;
        large_client_header_buffers 4 64k;
        client_max_body_size 75M;
        server_name app.website1.eu;
        location / {
                include uwsgi_params;
                proxy_ignore_client_abort on;
                uwsgi_ignore_client_abort on;
                client_body_buffer_size 64K;
                client_max_body_size 8M;
                uwsgi_pass 127.0.0.1:5002;
                #uwsgi_pass unix:///home/pathto/website1.sock;
                uwsgi_buffer_size 64k;
                uwsgi_buffers 8 64k;
                uwsgi_busy_buffers_size 64k;
                uwsgi_read_timeout 300;
                uwsgi_send_timeout 300;
                uwsgi_connect_timeout 60;
        }
        listen 443 ssl; # managed by Certbot
        listen [::]:443 ssl;
        ssl_certificate /etc/letsencrypt/live/app.website1.eu/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/app.website1.eu/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
        if ($host = app.website1.eu) {
                return 301 https://$host$request_uri;
        } # managed by Certbot
        listen 80;
        listen [::]:80;
        server_name app.website1.eu;
        return 404; # managed by Certbot
}

Server block of app2

server {
        client_header_buffer_size 64k;
        large_client_header_buffers 4 64k;
        client_max_body_size 75M;
        server_name app.website2.eu;

        location / {
                include uwsgi_params;
                proxy_ignore_client_abort on;
                uwsgi_ignore_client_abort on;
                client_body_buffer_size 64K;
                client_max_body_size 8M;
                uwsgi_pass 127.0.0.1:5003;
                #uwsgi_pass unix:///home/pathto/website2.sock;
                uwsgi_buffer_size 64k;
                uwsgi_buffers 8 64k;
                uwsgi_busy_buffers_size 64k;
                uwsgi_read_timeout 300;
                uwsgi_send_timeout 300;
                uwsgi_connect_timeout 60;
        }

    listen 443 ssl; # managed by Certbot
    listen [::]:443 ssl;
    ssl_certificate /etc/letsencrypt/live/app.website2.eu/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/app.website2.eu/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = app.website2.eu) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
        listen 80;
        listen [::]:80;
        server_name app.website2.eu;
        return 404; # managed by Certbot
}

Ini file of app1

[uwsgi]
module = wsgi:app
master = true
processes = 10
enable-threads = true
uid=ilias
gid=www-data
socket=127.0.0.1:5002
#socket = website1.sock
chmod-socket = 666
vacuum = true
buffer-size=65536
harakiri=60
ignore-sigpipe=true
ignore-write-errors=true
disable-write-exception
post-buffering=1
logger=file:/home/path1/website1/uwsgierror.log
http-keepalive=3000
die-on-term = true

Ini file of app2

[uwsgi]
module = wsgi:app
master = true
processes = 10
enable-threads = true
uid=ilias
gid=www-data
socket=127.0.0.1:5003
#socket = website2.sock
chmod-socket = 666
vacuum = true
buffer-size=65536
harakiri=60
ignore-sigpipe=true
ignore-write-errors=true
disable-write-exception
post-buffering=1
logger=file:/home/path1/website2/uwsgierror.log
http-keepalive=3000
die-on-term = true
  • I followed the tutorial at DigitalOcean and now none of the apps works as the error appearing in the nginx logs is *10 connect() failed (111: Connection refused) while connecting to upstream.
  • Before the problem, my app started normally as a systemd service on boot. Now it doesn't.
  • I can run normally the app with python app1.py as well as uwsgi --socket 0.0.0.0:5002 --protocol=http -w wsgi:app
  • When I tried to add upstream, my connection simply timed out.
  • I worked with http instead of socket files.

Can anyone please help me??


Solution

  • As always, it is a folder rights issue. If you follow the tutorial to the letter during the steps about creating the folders and creating virtual environment, then everything goes smoothly.