uWsgi是什么?

1、什么要用uWsgi?

&:因为nginx不支持wsgi协议,及无法直接调用py开发的webApp。
在nginx+uWsgi+flask的框架里,nginx代理+webServer,uWsgi是wsgiServer,flask是webApp。
nginx接收用户请求,并判定哪些转发到uWsgi,uWsgi再去调用pyWebApp。
ps:wsgiServer是新的提法,定义的更精准。

2、为什么有wsgi?

老外把来龙去脉讲得很清楚。直接看吧。
Why is WSGI necessary?
&:

  1. A traditional web server does not understand or have any way to run Python applications. In the late 1990s, a developer named Grisha Trubetskoy came up with an Apache module called mod_python to execute arbitrary Python code. For several years in the late 1990s and early 2000s, Apache configured with mod_python ran most Python web applications.
    传统的webServer是不能调用pyWebApp的(如tomcat上跑py?)。2000年左右,apache出现了mod_python,可以指定pyApp。

  2. However, mod_python wasn’t a standard specification. It was just an implementation that allowed Python code to run on a server. As mod_python’s development stalled and security vulnerabilities were discovered there was recognition by the community that a consistent way to execute Python code for web applications was needed.
    但mod_python没有成为标准,后来停止更新,并暴露出了安全问题。

  3. Therefore the Python community came up with WSGI as a standard interface that modules and containers could implement. WSGI is now the accepted approach for running Python web applications.
    py社区提出了WSGI协议,作为webServer与pyWebApp的交互协议。

2.1 WSGI servers learning checklist

  1. Understand that WSGI is a standard Python specification for applications and servers to implement.

  2. Pick a WSGI server based on available documentation and tutorials. Green Unicorn is a good one to start with since it’s been around for awhile.

  3. Add the WSGI server to your server deployment.

  4. Configure the web server to pass requests to the WSGI server for appropriate URL patterns.

  5. Test that the WSGI server responds to local requests but not direct requests outside your infrastructure. The web server should be the pass through for requests to and responses from the WSGI server.

参考:WSGI Servers

3、uwsgi+flask+nginx开发

  1. centOS安装python3.6
    参考:https://linux.cn/article-9680-1.html
    注意:
  • scl就可以python的虚拟环境
  • 使用指定环境
scl enable rh-python35 bash
  1. 安装uWsgi:
pip install uwsgi
  1. 安装flask:
pip install flask

flask默认自带了个webServer,但效率差。可用于开发调试,其自身也推荐使用专业的wsgiServer。

  1. 用uWsgi作webServer直接启动
    uWsgi配置,ini类型配置文件
[uwsgi]
http=0.0.0.0:8080
#the local unix socket file than commnuincate to Nginx 用于和 nginx 进行数据交互的端口
socket = 0.0.0.0:8001
#monitor uwsgi status 通过该端口可以监控 uwsgi 的负载情况
stats = 0.0.0.0:9000
wsgi-file=/pyProject/1ptc.py
callable=app
touch-reload=/pyProject/
#后台运行,并输出日志
daemonize = /pyProject/uwsgi.log
#保存进程id的文件,用来启动、停止、重新加载
pidfile=/pyProject/uwsgi.pid
  1. uwsgi管理
    启动uWsgi:uwsgi --ini uwsgi.ini
    停止uWsgi:uWsgi --stop uwsgi.pid #需要根据文件中的进程号去结束进程
    监控: uwsgitop 127.0.0.1 9000
uwsgi-2.0.18 - Tue Feb 25 22:37:35 2020 - req: 8000 - RPS: 0 - lq: 0 - tx: 773.4K
node: 127.0.0.1 - cwd: /pyProject - uid: 0 - gid: 0 - masterpid: 2534
 WID    %       PID     REQ     RPS     EXC     SIG     STATUS  AVG     RSS     VSZ     TX      ReSpwn  HC      RunT    LastSpwn
 1      100.0   2536    8000    0       0       0       idle    0ms     0       0       773.4K  1       0       5465.436        22:06:38

uwsgi的总结:

  1. 是专业的wsgiServer,用来接收webServer转发的请求,然后调用pyWebApp
  2. 也可以直接当做webServer,设置http参数。
  3. 通过调整工作进程数,实现性能的调整。
  4. 在nginx+uwsgi+flask中,或者在web程序框架中,不可或缺(或者使用类似的gcorn)

参考:
使用uwsgi部署应用
uwsgi配置详解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章