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

【场景篇】

  为了节省端口的占用,将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 hack


Note: ancient uWSGI versions used to support the so called “uwsgi_modifier1 30” approach. Do not do it. it is a really ugly hack


Note: 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 apps
mount = /app1=app1.py
mount = /app2=app2.py
; rewrite SCRIPT_NAME and PATH_INFO accordingly
manage-script-name = true

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

 

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

依葫芦画瓢

[uwsgi]
mount = /get_data=/home/yxgly/code/doraemon/DsBag/GET_DATA/Get_Data_Api.py
mount = /get_info=(此处省略,就不写了)
mount = /get_class=(此处省略,就不写了)
callable = app
manage-script-name = true
socket = /tmp/uwsgi_ini.sock

这里用的是ini格式的作为配置文件,所以启动方式稍微有些变化(当然,你也可以用xml来写)

uwsgi --ini myscg_conf.ini -d /tmp/uwsgi.log

然后只需在nginx层在配置一个‘/‘ 的location即可。

形如:

server {
        listen       8888;
        server_name  _;

        location / {
                include uwsgi_params;
                uwsgi_pass unix:///tmp/uwsgi_ini.sock;
        }
        access_log  /app/Nginx/logs/access_8888.log main;
        }

 

然后就可以使用

http://{ip}:8888/get_data/……

http://{ip}:8888/get_info/……

http://{ip}:8888/get_class/……

来访问了。

 

若有更好的建议,欢迎留言,持续改进?

转载于:https://www.cnblogs.com/soukingang/p/5694872.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值