確保一下軟件包安裝被安裝上:html
<pre> ubuntu: #apt-get install uwsgi-plugin-python nginx </pre>前端
簡要介紹一下:python
從一個 uwsgi 出錯試驗開始:nginx
<pre> def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"] </pre>web
<pre> /usr/bin/uwsgi_python -s 127.0.0.1:9090 --file hello.py --daemonize uwsgi.log </pre>apache
如今,打開網絡瀏覽器,在地址欄中輸入」http://127.0.0.1:8000「並回車,這時網頁中會顯示「The connection was reset……"當你不斷刷新瀏覽器,日誌文件 uwsgi.log 中應當能夠看到相似下面的信息ubuntu
<pre> invalid request block size: 21573 (max 4096)...skip Thu Feb 20 03:29:28 2014 - error parsing request invalid request block size: 21573 (max 4096)...skip Thu Feb 20 03:29:28 2014 - error parsing request invalid request block size: 21573 (max 4096)...skip Thu Feb 20 03:29:29 2014 - error parsing request ... </pre>後端
這表示,你在瀏覽器中提交的 http 請求被 uwsgi 給忽視了。若是你不斷的刷新那個頁面,終端中會不斷涌出該信息。雖然咱們獲得的是被重置的頁面,可是這足以證實 uwsgi 是一個 http 服務器。繼續在/etc/nginx/sites-available/default 中添加以下配置:瀏覽器
nginx-cgi 配置服務器
<pre> server { listen 80; server_name 127.0.0.1; location /cgi { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; } } </pre>
重啓服務 /etc/init.d/nginx restart
重啓nginx服務後,使用瀏覽器打開: 127.0.0.1/cgi 終於如願出現久違的 Hello World ,而不是「The connection was reset..."
uwsgi 實際上也是一個 http 服務器,只不過它只面向 python 網絡應用程序。雖然 uwsgi 也是 http 服務器,可是卻不能直接使用它部署 python web 應用程序,不然會出錯。
在本文中,uwsgi 所扮演的的角色是後端 http 服務器,nginx 扮演的角色是前端 http 服務器,hello.py 是客戶端應用程序。 用戶從網頁瀏覽器中發出請求,nginx 服務器收到請求後,會經過它的 uwsgi 模塊將用戶的請求轉發給 uwsgi 服務器,uwsgi 服務器處理完畢後將結果返回給 nginx,瀏覽器將最終的結果展示給用戶。