使用 uwsgi + nginx 将 Flask 应用程序与 Django 应用程序一起部署

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

我正在尝试在当前部署了 django 应用程序的服务器中部署一个简单的 flask 应用程序。

django 应用程序工作正常,但 flask 应用程序显示 404 错误,尽管我已经完成了正确的设置并且我没有在日志中看到任何错误。

目前,这是我的 app2.ini 文件

我的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 配置文件:

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;
    }
}

我的app2.ini文件:

[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

树:

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

当我尝试访问我的站点时,一切似乎都正常。com/flask这是 uwsgi 日志文件的输出

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)

当我尝试访问我的网站时 nginx.log 文件。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"

如果您看到什么请告诉我,我一直在努力解决它,但不知道我还能做什么。

好吧,如果其他人遇到这个问题,我设法通过在 /flask 位置包含这个 conf 来做到这一点:

location /flask {
    rewrite ^/flask(.*)  break; # IMPORTANT!
    include uwsgi_params;
    uwsgi_pass unix:/home/app2/app/flaskapp/uwsgi.sock;
}