Deploy a flask application alongside a Django application with uwsgi + nginx

Question:

I’m trying to deploy a simple flask app in a server where is currently deployed a django app.

The django app works okay, but with the flask app shows 404 error, although I’ve done the correct settings and I don’t see any error shown in the logs.

Currently, this is my app2.ini file

my app.py:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(host='::')

wsgi.py

from flaskapp.app import app

if __name__ == "__main__":
    app.run()

nginx conf:

server {
    listen *:80;
    listen [::]:80;

    server_name mysite.com;

    # Let's Encrypt
    location /.well-known/acme-challenge/ {
        root /var/www/html;
    }

    # Everything else -> SSL
    location / {
        return 301 https://$host$request_uri;
    }
}


server {
    listen 443;
    listen [::]:443;

    server_name mysite.com;

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

    ssl on;
    ssl_certificate      /etc/letsencrypt/live/mysite.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/mysite.com/privkey.pem;

    client_max_body_size 256m;

    location / {
        try_files $uri @uwsgi;
    }

    location @uwsgi {
        include uwsgi_params;
        uwsgi_pass unix:/home/app/app/uwsgi.sock;
    }

    location /flask/ {
        include uwsgi_params;
        uwsgi_pass unix:/home/app2/app/flaskapp/uwsgi.sock;
    }
}

my app2.ini file:

[uwsgi]
socket = /home/app2/app/flaskapp/uwsgi.sock
chdir = /home/app2/app
wsgi-file = flaskapp/wsgi.py
touch-reload = flaskapp/wsgi.py
venv = /home/app2/pyvenv
processes = 4
threads = 2
chmod-socket = 666
daemonize=true
vacuum = true
uid = app2
gid = app2
callable = app

tree:

flaskapp
├── app.py
├── __init__.py
├── __pycache__
│   ├── app.cpython-35.pyc
│   └── __init__.cpython-35.pyc
├── uwsgi.sock
└── wsgi.py

Everything seems to work, when I try to go to mysite.com/flask this is the output of the uwsgi log file

Tue Jun  4 12:58:45 2019 - *** Starting uWSGI 2.0.14-debian (64bit) on [Tue Jun  4 12:58:45 2019] ***
Tue Jun  4 12:58:45 2019 - compiled with version: 6.3.0 20170516 on 17 March 2018 15:41:47
Tue Jun  4 12:58:45 2019 - os: Linux-4.9.0-9-amd64 #1 SMP Debian 4.9.168-1+deb9u2 (2019-05-13)
Tue Jun  4 12:58:45 2019 - nodename: devuan
Tue Jun  4 12:58:45 2019 - machine: x86_64
Tue Jun  4 12:58:45 2019 - clock source: unix
Tue Jun  4 12:58:45 2019 - pcre jit disabled
Tue Jun  4 12:58:45 2019 - detected number of CPU cores: 1
Tue Jun  4 12:58:45 2019 - current working directory: /
Tue Jun  4 12:58:45 2019 - writing pidfile to /run/uwsgi/app/app2/pid
Tue Jun  4 12:58:45 2019 - detected binary path: /usr/bin/uwsgi-core
Tue Jun  4 12:58:45 2019 - setgid() to 1001
Tue Jun  4 12:58:45 2019 - setuid() to 1001
Tue Jun  4 12:58:45 2019 - chdir() to /home/app2/app
Tue Jun  4 12:58:45 2019 - your processes number limit is 7928
Tue Jun  4 12:58:45 2019 - your memory page size is 4096 bytes
Tue Jun  4 12:58:45 2019 - detected max file descriptor number: 1024
Tue Jun  4 12:58:45 2019 - lock engine: pthread robust mutexes
Tue Jun  4 12:58:45 2019 - thunder lock: disabled (you can enable it with --thunder-lock)
Tue Jun  4 12:58:45 2019 - uwsgi socket 0 bound to UNIX address /run/uwsgi/app/app2/socket fd 3
Tue Jun  4 12:58:45 2019 - uwsgi socket 1 bound to UNIX address /home/app2/app/flaskapp/uwsgi.sock fd 5
Tue Jun  4 12:58:45 2019 - Python version: 3.5.3 (default, Sep 27 2018, 17:25:39)  [GCC 6.3.0 20170516]
Tue Jun  4 12:58:45 2019 - Set PythonHome to /home/app2/pyvenv
Tue Jun  4 12:58:45 2019 - Python main interpreter initialized at 0x5643c89a0f90
Tue Jun  4 12:58:45 2019 - python threads support enabled
Tue Jun  4 12:58:45 2019 - your server socket listen backlog is limited to 100 connections
Tue Jun  4 12:58:45 2019 - your mercy for graceful operations on workers is 60 seconds
Tue Jun  4 12:58:45 2019 - mapped 415360 bytes (405 KB) for 8 cores
Tue Jun  4 12:58:45 2019 - *** Operational MODE: preforking+threaded ***
Tue Jun  4 12:58:46 2019 - WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x5643c89a0f90 pid: 17448 (default app)
Tue Jun  4 12:58:46 2019 - *** uWSGI is running in multiple interpreter mode ***
Tue Jun  4 12:58:46 2019 - spawned uWSGI master process (pid: 17448)
Tue Jun  4 12:58:46 2019 - spawned uWSGI worker 1 (pid: 17487, cores: 2)
Tue Jun  4 12:58:46 2019 - spawned uWSGI worker 2 (pid: 17488, cores: 2)
Tue Jun  4 12:58:46 2019 - spawned uWSGI worker 3 (pid: 17491, cores: 2)
Tue Jun  4 12:58:46 2019 - spawned uWSGI worker 4 (pid: 17492, cores: 2)
[pid: 17491|app: 0|req: 1/1] 2a0a:e5c1:115::42 () {44 vars in 767 bytes} [Tue Jun  4 12:58:51 2019] GET /flask/ => generated 232 bytes in 14 msecs (HTTP/1.1 404) 2 headers in 72 bytes (2 switches on core 0)

The nginx.log file when i try to reach mysite.com/flask:

2a0a:e5c1:115::42 - - [04/Jun/2019:13:14:58 +0000] "GET /flask/ HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"

Let me know if you see something, I’ve been trying to solve it but no clue what else can I do.

Asked By: William Colmenares

||

Answers:

Well In case anyone else face this issue I managed to do it by including this conf in the /flask location:

location /flask {
    rewrite ^/flask(.*) $1 break; # IMPORTANT!
    include uwsgi_params;
    uwsgi_pass unix:/home/app2/app/flaskapp/uwsgi.sock;
}
Answered By: William Colmenares
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.