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配置詳解

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