【发布时间】:2016-09-29 01:10:36
【问题描述】:
我找到了很多例子来解释我们如何使用 uwsgi 和 Emperor 模式来实现多个应用程序的部署。这对我来说意味着:多个应用程序文件夹,每个应用程序有一个附庸(ini、socket、application.py)。
我还没有找到只有一个应用文件夹和多个附庸的配置示例。这应该允许我为多租户应用程序提供服务(每个客户都有自己的数据库)。我用两个实例对此进行了测试。这“似乎”运作良好。
- 这是一个好的做法还是您有类似的设置?
- 这是否提供了 vassals 实例之间的完全隔离?
这是我的设置。我正在使用 nginx/uwsgi/python 堆栈(我使用emperor_pg module)。此设置允许 uwsgi 为每个客户 A 和 B 生成一个附庸。客户正在使用 url:customerN.mydomain.com/fe1/web
Nginx 配置:
# This virtual host catches all incoming traffic from port 80 (security should be considered if not talking on local
# network)
server {
listen 80;
# We capture here the subdomain. It is used to designate a customer entity.
server_name ~^(?<subdomain>.+)\.mydomain\.fr$;
# We use a pattern for creating sockets name and path.
# This allows to spawn vassals automatically by detecting changes in the vassals pg table (emperor_pg)
# Pattern used is : /tmp/$subdomain$appname.sock
location ~favicon\.ico$ {
root /opt/app-current/web/;
}
location ~^\/(?<app_name>.+)\/web\/ {
root /opt/web-content/$subdomain/;
}
location ~^\/(?<app_name>.+)\/ {
# Routing to socket designated by standard pattern
include uwsgi_params;
uwsgi_pass unix://tmp/$subdomain$app_name.sock;
}
# When calling root of entity's subdomain, we launch the default app by routing traffic to index.socket
location / {
include uwsgi_params;
uwsgi_pass unix://tmp/$subdomain.sock;
}
}
皇帝暴发户脚本使用 Emperor_pg :
# Emperor configuration upstart script in `/etc/init/uwsgi.conf` :
# uWSGI - Manage uWSGI Application Server
description "uWSGI Emperor Mode"
start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]
respawn
# We use pg mode. This allows to scan a postgresql database.
# requires sudo apt-get install uwsgi-plugin-emperor-pg
exec /usr/bin/uwsgi --uid www-data --gid www-data --plugin emperor_pg --emperor "pg://host=dbserver.com.com user=saasautomator dbname=saasautomator;SELECT name,config,ts,uid,gid,socket FROM vassals" --logto /var/log/uwsgi.log
还有一个附庸的例子 数据库中的 conf 文件。他们正在使用相同的应用程序文件夹:
saasautomator=> SELECT * FROM vassals;
idvassals | name | config | ts | uid | gid | socket
-----------+---------------------------------+----------------------------------------------------------------+---------------------+-------+-------+---------------------------------------
3 | customerAfe1.ini | [uwsgi] +| 2004-10-19 10:23:54 | uwsgi | uwsgi | /tmp/customerAfe1.sock
| | master = true +| | | |
| | vaccum = true +| | | |
| | chdir = /opt/app/ +| | | |
| | plugins = python +| | | |
| | wsgi-file = /opt/app/fe1/application.py +| | | |
| | processes = 4 +| | | |
| | threads = 2 +| | | |
| | stats = 127.0.0.1:9191 | | | |
4 | customerBfe1.ini | [uwsgi] +| 2004-10-19 10:23:55 | uwsgi | uwsgi | /tmp/customerBfe1.sock
| | master = true +| | | |
| | vaccum = true +| | | |
| | chdir = /opt/app/ +| | | |
| | plugins = python +| | | |
| | wsgi-file = /opt/app/fe1/application.py +| | | |
| | processes = 4 +| | | |
| | threads = 2 +| | | |
| | stats = 127.0.0.1:9192 | | | |
(2 rows)
谢谢!
【问题讨论】:
-
我不确定这个问题是否适合stackoverflow,但无论如何我都会回答...
-
感谢您的帮助。抱歉用错地方了。你会在哪里问这个问题?
标签: python nginx uwsgi multi-tenant saas