今天看了一篇關(guān)于WSGI的英語文章,寫的很清晰,總結(jié)一下以便日后翻閱。
WSGI Servers
Web Server Gateway Interface服務(wù)器實(shí)際上把WSGI接口包裝成了服務(wù)器,可以用來運(yùn)行Python web applications。
Why is WSGI necessary?
傳統(tǒng)的網(wǎng)頁服務(wù)器沒有辦法運(yùn)行python應(yīng)用。在20世紀(jì)末期,Grisha Trubetskoy開發(fā)了一個(gè)Apache模塊,mode_python來運(yùn)行任意的python代碼。但是這樣也只是實(shí)現(xiàn)了從無到有的實(shí)現(xiàn),并不是一個(gè)標(biāo)準(zhǔn)的約定。于是Python社區(qū)發(fā)明了WSGI作為標(biāo)準(zhǔn)的接口。這個(gè)接口用于Web Server 和 Python web application之間的通信連接。

如圖中所示,一個(gè)WSGI服務(wù)器會(huì)根據(jù)PEP 3333 standard,調(diào)用Python app。
WSGI's Purpose
那么為什么要使用WSGI,而不是讓web app直接和web server進(jìn)行通信呢?
WSGI 使你的Web App更加靈活:開發(fā)者或許會(huì)替換他們技術(shù)站中的元素。比如我現(xiàn)在不想使用Gunicorn了,換成了uWSGI了。那么我不需要去修改Web App的接口,因?yàn)镚unicorn 和 uWSGI都是基于WSGI接口實(shí)現(xiàn)的。
就像在PEP(Python Enhancement Proposal) 3333 中提到的:
由于WSGI接口的廣泛使用,開發(fā)者選擇Web框架的選擇可以不在那么依賴于Web Server了。開發(fā)者只需要選擇合適的Web Server和這個(gè)Server 支持的CGI接口,就可以進(jìn)行自由地配對(duì)。另一方面讓術(shù)業(yè)有專攻,Web Server開發(fā)者可以專注于自己的領(lǐng)域,Web App開發(fā)者亦是。WSGI服務(wù)器提高了Web App 的適應(yīng)性Scaling: 服務(wù)成千上萬的請(qǐng)求是WSGI服務(wù)器的工作,而不是Web App的工作。Web App只專注于邏輯處理而已。如果分發(fā)這些請(qǐng)求,使之得到高效的處理是WSGI的工作之一。

上圖是Web Browser,Web Server 和 WSGI Server之間的通信流程。
WSGI就是一個(gè)用來運(yùn)行Python代碼的標(biāo)準(zhǔn)接口。而作為一個(gè)網(wǎng)頁開發(fā)者,你所需要知道的是:
- WSGI代表了Web Server Gateway Interface.
- WSGI服務(wù)器是一個(gè)單獨(dú)的進(jìn)城,跑在不同的端口號(hào)上,不同于Web Server。
- 你需要配置好你的Web Server,讓它把網(wǎng)絡(luò)請(qǐng)求發(fā)送到WSGI服務(wù)器。等WSGI服務(wù)器處理完請(qǐng)求,再把Response返回到Web Server。
如果你是用的是目前流行的框架,比如Django和Flask。那么你不需要擔(dān)心WSGI實(shí)現(xiàn)的問題,這些框架都已經(jīng)實(shí)現(xiàn)好了。同樣地,如果你使用類似于Gunicorn和uWSGI之一類的WSGI服務(wù)器,你也不需要擔(dān)心WSGI的實(shí)現(xiàn)問題。它們都是基于WSGI實(shí)現(xiàn)的。你只需要進(jìn)行合理地配置,讓它們知道如何與彼此之間進(jìn)行通信就可以了。
Example web server configuration
一個(gè)Web Server的配置中會(huì)寫明了什么網(wǎng)絡(luò)請(qǐng)求應(yīng)該被轉(zhuǎn)送到WSGI 服務(wù)器。
例如,一個(gè)Nginx的web服務(wù)器寫明了,Nginx需要處理靜態(tài)資源,比如images,JavaScript和CSS文件。這些文件一般位于/static目錄下面。除此之外的請(qǐng)求都轉(zhuǎn)交給WSGI位于8000端口的服務(wù)器。
# this specifies that there is a WSGI server running on port 8000
upstream app_server_djangoapp {
server localhost:8000 fail_timeout=0;
}
# Nginx is set up to run on the standard HTTP port and listen for requests
server {
listen 80;
# nginx should serve up static files and never send to the WSGI server
location /static {
autoindex on;
alias /srv/www/assets;
}
# requests that do not fall under /static are passed on to the WSGI
# server that was specified above running on port 8000
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_djangoapp;
break;
}
}
}
WSGI Servers
目前比較流向的WSGI服務(wù)器有:
Green Unicorn is a pre-fork worker model based server ported from the Ruby Unicorn project.
uWSGI is gaining steam as a highly-performant WSGI server implementation.
mod_wsgi is an Apache module implementing the WSGI specification.
CherryPy is a pure Python web server that also functions as a WSGI server.