uwsgi介紹
uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI服務器進行交換。
要注意 WSGI / uwsgi / uWSGI 這三個概念的區分。
WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx,uWSGI等服務器)與web應用(如用Flask框架寫的程序)通信的一種規范。
uwsgi是一種線路協議而不是通信協議,在此常用于在uWSGI服務器與其他網絡服務器的數據通信。
而uWSGI是實現了uwsgi和WSGI兩種協議的Web服務器。
uwsgi協議是一個uWSGI服務器自有的協議,它用于定義傳輸信息的類型(type of information),每一個uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣東西。
安裝uwsgi
pip install uwsgi
uwsgi不支持windows
測試啟動
創建測試文件并寫入:
def application(environ, start_response): status = '200 OK' output = 'Hello World! powerde by wsgi' response_headers = [('Content-type', 'text/plain'),('Content-Length', str(len(output)))] start_response(status, response_headers)return [output.encode('utf8'),]
執行命令:
uwsgi --http :8080 --file test.py
瀏覽器訪問該端口,正常情況下能得到輸出。
用 uwsgi 啟動django
uwsgi --http :8080 --file django_project/wsgi.py
頁面能訪問,但是靜態文件無法加載,需要
uwsgi --http :8080 --file django_project/wsgi.py --static-map=/static=static
靜態文件就能加載了。
參數說明:
http 這個就和 runserver 一樣指定 IP 端口 file 這個文件就里有一個反射,如果你在調用他的時候沒有指定Web Server就使用默認的 static 做一個映射,指定靜態文件uwsgi配置文件啟動django項目
uwsgi 支持的參數還挺多的,可以將他們寫在配置文件中。在項目同級目錄創建 uwsgi.ini 文件:
# uwsig使用配置文件啟動[uwsgi]# 項目目錄chdir=/opt/webvirtcloud/# 指定項目的applicationmodule=webvirtcloud.wsgi:application# 指定sock的文件路徑 socket=/tmp/uwsgi.sock# 進程個數 workers=5pidfile=/tmp/uwsgi.pid# 指定IP端口 http=0.0.0.0:8080 # 如果和ngxin結合,本行注釋掉# 指定靜態文件static-map=/static=/opt/webvirtcloud/static# 啟動uwsgi的用戶名和用戶組uid=rootgid=root# 啟用主進程master=true# 自動移除unix Socket和pid文件當服務停止的時候vacuum=true# 序列化接受的內容,如果可能的話thunder-lock=true# 啟用線程enable-threads=true# 設置自中斷時間harakiri=30# 設置緩沖post-buffering=4096# 設置日志目錄daemonize=/var/log/uwsgi.log
更多參數可見: https://uwsgi-docs.readthedocs.io/en/latest/Options.html
|
新聞熱點
疑難解答