Django和Flask在同一个nginx服务器上

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

当我访问mysite.com的时候,我的主站点上正在运行一个Djago应用程序。但是,我希望www.example.com运行一个单独的Flask应用程序。我可以设置两个nginx站点启用配置文件,并在不同的端口上运行每个应用程序,但是,由于各种原因,mysite.com/flaskapp to run a separate Flask application. I'm able to set up two nginx site-enabled config files and run each app on a different port but, for various reasons, I'd like to run them all on the same port (if possible). When I configure my flaskapp/ location in my nginx server file I get a 404 error.
下面是我的管理员配置文件:

[program:MYSITE]
command=/var/www/html/MYSITE/prodenv/bin/gunicorn --workers 3 --bind unix:/var/www/html/MYSITE/public_html/MYSITE.sock MYSITE.wsgi
directory=/var/www/html/MYSITE/public_html
autostart=true
autorestart=true
stderr_logfile=/var/log/MYSITE.err.log
stdout_logfile=/var/log/MYSITE.out.log

[program:FLASKAPP]
directory=/var/www/html/MYSITE/public_html/FLASKAPP/api
command=/var/www/html/MYSITE/public_html/FLASKAPP/venv/bin/gunicorn --workers 3 --bind unix:/var/www/html/MYSITE/public_html/FLASKAPP/api/FLASKAPP.sock FLASKAPP:app
autostart=true
autorestart=true
stderr_logfile=/var/log/FLASKAPP.err.log
stdout_logfile=/var/log/FLASKAPP.out.log

我的nginx站点启用文件:

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

    server_name MYSITE;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        root /var/www/html/MYSITE/public_html;
        expires 30d;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/html/MYSITE/public_html/MYSITE.sock;
    }

    location /FLASKAPP/ {
        include proxy_params;
        proxy_pass http://unix:/var/www/html/MYSITE/public_html/FLASKAPP/api/FLASKAPP.sock; 
    }
}

我写了一个函数来检查请求URL

@app.errorhandler(404)
    def page_not_found(error):
         return 'This route does not exist {}'.format(request.url), 404

该函数返回:
此路由不存在http://MYSITE/FLASKAPP
flask 应用程序显然在工作,但是路由搞砸了,我怎么才能修复这个问题?

l7wslrjt

l7wslrjt1#

我想通了,我重写了网址去掉了子目录,现在一切都好了:

rewrite ^/FLASKAPP(.*)$ $1 break;

相关问题