nginx不支援cgi,也就是nginx 不能直接執行外部指令碼。但是nginx支援fastcgi。所以為了在nginx運行fastcgi,我們採用flup,flup是fastcgi的一種實現。這裡是官網https://www.saddi.com/software/flup/ 我用最簡單的模式就是 nginx+flup。
* 第一步安裝flup。目前的目錄轉為\python安裝目錄\Lib\site-packages 但後用easy_install安裝flup的egg檔案。
* 第二步就是配置nginx.conf檔案,我用最簡單的配置:
location ~ ^/test { include fastcgi_params; fastcgi_pass 127.0.0.1:10080; }
* 第三步也是最蛋疼的一部就是測試代碼。用的pytho你上的範例程式碼:
from cgi import escape
import sys, os
from flup.server.fcgi import WSGIServer
def app(environ, start_response):
start_response(‘200 OK’, [(‘Content-Type’, ‘text/html’)])
yield '<h1>FastCGI Environment</h1>'yield '<table>'for k, v in sorted(environ.items()): yield '<tr><th>%s</th><td>%s</td></tr>' % (escape(k), escape(v))yield '</table>'WSGIServer(app).run()
結果一直提示錯誤:
sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET,AttributeError: 'module' object has no attribute 'fromfd'
網上也有說要修改fcgi_base.py的,我覺得不對,於是找到有人在最後一行代碼中指定地址和連接埠。之前也一直在像連接埠在哪裡設定。修改之後正常運行,至少能監聽我要的連接埠。
最後一行修改為:
WSGIServer(app, bindAddress=('127.0.0.1',10080)).run()
估計這就是最簡單的python+fcgi模式。Django之類的估計是在flup上的加強版本。不懂。