如何在Nginx上托管的单个域下托管多个Flask应用程序? [英] How to host multiple flask apps under a single domain hosted on nginx?

查看:63
本文介绍了如何在Nginx上托管的单个域下托管多个Flask应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现的目标:

我有一个托管mydomain.com的Nginx Web服务器.当有人在其客户端中键入domain.com时,我希望我的服务器从/var/www/mydomain/提供index.html.当他们键入mydomain.com/flaskapp1时,他们应该看到flaskapp1.当他们键入mydomain.com/flaskapp2时,他们应该看到flaskapp2.

I have an nginx web server hosting mydomain.com. When someone types my domain.com into their client I would like my server to serve index.html from /var/www/mydomain/ When they type mydomain.com/flaskapp1 they should see flaskapp1. When they type mydomain.com/flaskapp2 they should see flaskapp2.

我已经设法通过此处的教程 http://www.markjberger.com/flask-with-virtualenv-uwsgi-nginx/,但是在尝试实现为两个单独的Flask应用程序提供服务时,我遇到了困难.当我尝试在浏览器中使用mydomain.co.uk/flaskapp或mydomain.co.uk/flaskapp2访问任何一个烧瓶应用程序时,都没有看到flask应用程序,而是收到404消息.

I have managed to get one or the other flask apps served using the tutorial here http://www.markjberger.com/flask-with-virtualenv-uwsgi-nginx/ but when trying to implement serving two separate flask apps I run into difficulty. Instead of seeing the flask app I get a 404 message when I try to access either of the flask apps with mydomain.co.uk/flaskapp or mydomain.co.uk/flaskapp2 in a browser.

这是我到目前为止所拥有的:

This is what I have so far:

猫/etc/nginx/sites-available/mydomain.co.uk

server {
       listen 80;

        server_name www.mydomain.co.uk mydomain.co.uk;

        location / {
               root /var/www/html/;
               index index.html index.htm;
        }

        location /flaskapp {
                include uwsgi_params;
                uwsgi_pass unix:/tmp/flaskapp.sock;
        }

         location /flaskapp2 {
              include uwsgi_params;
              uwsgi_pass unix:/tmp/flaskapp2.sock;
         }
 }

以上conf文件已被SIM链接到/etc/nginx/sites-enabled .

The above conf file has been sim linked into /etc/nginx/sites-enabled.

猫/etc/uwsgi/apps-available/flaskapp.ini

[uwsgi]
vhost = true
socket = /tmp/flaskapp.sock
venv = /var/www/flaskapp/venv
chdir = /var/www/flaskapp
module = flaskapp
callable = app

猫/etc/uwsgi/apps-available/flaskapp2.ini

[uwsgi]
vhost = true
socket = /tmp/flaskapp2.sock
venv = /var/www/flaskapp2/venv
chdir = /var/www/flaskapp2
module = flaskapp2
callable = app

两个.ini文件都已符号链接到/etc/uwsgi/apps-enabled 中.UWSGI可以正常重启,并且没有任何问题,并且可以正常运行.flaskapp.sock和flaskapp2.sock都属于www-data

Both .ini files have been symlinked into /etc/uwsgi/apps-enabled. UWSGI restarts fine without any issues and is up and running. Both flaskapp.sock and flaskapp2.sock are owned by www-data

猫/var/www/flaskapp/flaskapp.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World from flaskapp1!'

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

猫/var/www/flaskapp2/flaskapp2.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World from flaskapp2!'

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

猫/var/www/mydomain.co.uk/index.html

<!DOCTYPE html>
<html>
<body>

<h1>mydomain.co.uk</h1>

<p>This is the index page of my domain.co.uk</p>

</body>
</html>

两个虚拟环境均已安装Flask,并将使用开发服务器运行flask应用程序.

Both virtual environments have flask installed and will run the flask apps using the development server.

我希望这是我错过的简单事情.

I hope it's something simple that I've missed.

推荐答案

查看NGINX的uwsgi文档此处.

Looking at the uwsgi documentation for NGINX here.

特别是:

不幸的是,nginx无法将PATH_INFO相应地重写为SCRIPT_NAME.因此,您需要指示uWSGI进行映射所谓的挂载点"中的特定应用,然后重写SCRIPT_NAME和PATH_INFO自动:

Unfortunately nginx is not able to rewrite PATH_INFO accordingly to SCRIPT_NAME. For such reason you need to instruct uWSGI to map specific apps in the so called "mountpoint" and rewrite SCRIPT_NAME and PATH_INFO automatically:

更改我的 flaskapp.ini flaskapp2.ini 文件以包含应用程序的挂载点并打开manage-script-name变量已奏效.

Changing my flaskapp.ini and flaskapp2.ini files to contain the mount points for the apps and turning on the manage-script-name variable has worked.

猫/etc/uwsgi/apps-available/flaskapp.ini

[uwsgi]
vhost = true
socket = /tmp/flaskapp.sock
venv = /var/www/flaskapp/venv
chdir = /var/www/flaskapp
module = flaskapp
callable = app
mount = /flaskapp=flaskapp.py
manage-script-name = true

猫/etc/uwsgi/apps-available/flaskapp2.ini

[uwsgi]
vhost = true
socket = /tmp/flaskapp2.sock
venv = /var/www/flaskapp2/venv
chdir = /var/www/flaskapp2
module = flaskapp2
callable = app
mount = /flaskapp2=flaskapp2.py
manage-script-name = true

现在两个烧瓶应用程序都根据需要通过nginx通过uwsgi运行.

And now both flask apps are running via uwsgi through nginx as required.

这篇关于如何在Nginx上托管的单个域下托管多个Flask应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆