简述uwsgi使用一个服务启动多个文件的用法

来源:互联网 发布:关键词排列组合软件 编辑:程序博客网 时间:2024/05/01 21:29

【场景篇】

  为了节省端口的占用,将N个flask应用服务——每个对应一个文件(web.py、django也一样)合并为一个端口服务来启用

 

【寻思篇】

  通常的做法:每个文件配置一个xml 或者 ini文件,然后依次启动uwsgi(uwsgi -x {xml文件名}),形如:

复制代码
<uwsgi>     <wsgi-file>/home/yxgly/code/doraemon/DsBag/GET_DATA/Get_Data_Api.py</wsgi-file>     <callable>app</callable>     <socket>/tmp/uwsgi1.sock</socket>     <stats>/tmp/stats.socket1</stats>     <master/>     <enable-threads>true</enable-threads>     <workers>1</workers>     <threads>1</threads>     <processes>1</processes></uwsgi>
复制代码
uwsgi -x {xml} -d /tmp/uwsgi.log

但这样会导致启动的uwsgi太多,不便于管理。

 

  稍高级一点的用法,可以使用“皇帝”(emperor),具体用法:将所有的xml统一放到一个目录里(例如:放到这个目录下 mysgi_conf),进行一并启动:

uwsgi --emperor mysgi_conf/ --logto /tmp/uwsgi.log

这样启动了之后,每个文件都有有个与之对应的socket文件

 

然后在nginx上配置多条location来做好走向,形如:

复制代码
server {        listen       8888;        server_name  _;        location /get_data {                include uwsgi_params;                uwsgi_pass unix:///tmp/uwsgi1.sock;        }        location /get_info {                include uwsgi_params;                uwsgi_pass unix:///tmp/uwsgi2.sock;        }        location /get_class {                include uwsgi_params;                uwsgi_pass unix:///tmp/uwsgi3.sock;        }        ……………………………………            access_log  logs/access_8888.log main;        }
复制代码

但这种会出现种种错误,若是只配置“location /" 就会正常,但我的“需求”是在一个端口下(8888)做好所有的事情……

找谷哥、度娘……找到了一篇

https://jawher.me/2012/03/16/multiple-python-apps-with-nginx-uwsgi-emperor-upstart/

坑爹的是按照他指导的样子去做,还是不奏效

与此同时,更让我困惑的是文中所提到的“uwsgi_modifier1”的用法,我又没有完全“领会”明白,最后搜到了这样一段解释,就算稍微明白了一些。

uwsgi_modifier1 value 30 means:Standard WSGI request followed by the HTTP request body. The PATH_INFO is automatically modified, removing the SCRIPT_NAME from it.

 

【解决篇】

在网上搜了很多关于这个问题的解决思路,但是没有找到一个“合适”的答案(百变不离其中的都是抄来抄去,没有任何一个奏效的)

于是 决定还是阅读更权威的官方文档

http://uwsgi-docs.readthedocs.io/en/latest/Nginx.html (这一篇介绍与nginx交互的页面,答案就在此)

复制代码
重要的事情说三遍!Note: ancient uWSGI versions used to support the so called “uwsgi_modifier1 30” approach. Do not do it. it is a really ugly hackNote: ancient uWSGI versions used to support the so called “uwsgi_modifier1 30” approach. Do not do it. it is a really ugly hackNote: ancient uWSGI versions used to support the so called “uwsgi_modifier1 30” approach. Do not do it. it is a really ugly hack
复制代码

简言之,不要用“uwsgi_modifier1“了!

再看上下文,发现了个更有“意思”的用法

复制代码
[uwsgi]socket = 127.0.0.1:3031; mount appsmount = /app1=app1.pymount = /app2=app2.py; rewrite SCRIPT_NAME and PATH_INFO accordinglymanage-script-name = true
复制代码

大家看到这里也知道如何搞定了吧……

 

对,最后的样子就是这样子

0 0
原创粉丝点击