使用uwsgi + nginx在Django应用程序旁边部署一个flask应用程序

wlwcrazw  于 2023-03-01  发布在  Nginx
关注(0)|答案(1)|浏览(182)

我正在尝试在一个服务器上部署一个简单的flask应用程序,而这个服务器上目前部署了一个django应用程序。
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

当我尝试访问www.example.com时,一切似乎都正常mysite.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)

当我尝试访问www.example.com时,显示nginx.log文件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"

让我知道如果你看到的东西,我一直试图解决它,但没有线索,我还能做什么。

9bfwbjaz

9bfwbjaz1#

如果其他人遇到这个问题,我设法在/flask位置包含这个conf:

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

相关问题