Howto WSGI¶
WSGI 本身不是一个应用程序,也不是一种通信“方法”(例如 FastCGI、SCGI 或“CGI”);它是一种协议,规定了 Web 服务器应向 Web 应用程序发送哪些信息(因此它基本上是 CGI 的扩展,用于应用程序而不是简单的脚本),详情请参阅 http://wsgi.org/wsgi/What_is_WSGI。
大多数 Python Web 应用程序都使用 WSGI,而 Python 的 WSGI 处理器支持将应用程序作为简单的 Web 服务器、FastCGI 或 SCGI 应用程序运行。
我们推荐运行 WSGI Web 应用程序的方式是使用 FastCGI,本文将对此进行描述。
Lighttpd 配置¶
WSGI 要求将 URL 拆分为“应用程序路径”(SCRIPT_NAME)和剩余部分(PATH_INFO)——因此 Web 服务器会检测应用程序路径,并告知应用程序处理剩余部分。这使您可以在不同的主机名和路径上托管同一个 WSGI 应用程序,例如您可以在 http://wsgi.example.com/ 和 http://www.example.com/wsgi/ 上托管它,而无需为您的 WSGI 应用程序进行配置。
因此,您必须以某种方式将 URL 拆分为所需的各个部分,我们为此使用 Lua 动作。
您可以在我们的代码库中找到其源代码,source:contrib/core.lua(源代码中包含示例的文档);请参阅 mod_core。
以下是 http://xcache.lighttpd.net/ 的配置示例(Trac 作为 WSGI 应用程序运行)
setup { module_load ( "mod_expire", "mod_fastcgi", "mod_vhost", "mod_lua" ); lua.plugin "core.lua"; } var.vhosts = []; # ... var.vhosts = var.vhosts + [ "xcache.lighttpd.net" => { docroot "/var/www/servers/xcache.lighttpd.net"; alias ( "/trac-static/" => "/usr/share/trac/htdocs", "/chrome/common/" => "/usr/share/trac/htdocs" ); if request.path =~ "\.(css|png|js|gif)$" { expire "access 1 week"; } if physical.is_file { header.add ("X-cleanurl", "hit"); } else { header.add ("X-cleanurl", "miss"); core.wsgi ( "", { fastcgi "unix:/var/run/lighttpd/sockets/xcache.lighttpd.net.sock"; } ); } } ]; # ... vhost.map var.vhosts;
启动您的 WSGI 应用程序¶
请参阅 Howto_Rails,了解一般如何进行启动。对于 Trac,最终的 spawn-fcgi 行可能看起来像这样
exec /usr/bin/spawn-fcgi -n -u www-trac -U www-data -s /var/run/lighttpd/sockets/xcache.lighttpd.net.sock -- /usr/share/trac/web/fcgi_frontend.py
HowTo Python WSGI for lighttpd 1.4.x¶
https://redmine.lighttpd.ac.cn/projects/lighttpd/wiki/HowToPythonWSGI