Project

General

Profile

Howto WSGI » History » Revision 9

Revision 8 (stbuehler, 2014-05-27 20:01) → Revision 9/11 (stbuehler, 2014-05-27 20:02)

h1. Howto WSGI 

 WSGI is not an application and not a communication "method" itself (like FastCGI, SCGI or "CGI"); it is a protocol, what informations the webserver should send to an web application (so it it basically is an CGI extension for applications instead of simple scripts), see http://wsgi.org/wsgi/What_is_WSGI. 
 Most python web applications use WSGI, and the wsgi handlers for python provides support for running the application as a simple webserver, FastCGI or SCGI application. 
 Our recommended way to run WSGI web applications is with FastCGI, and that is what is described here. 

 h2. Lighttpd config 

 WSGI wants the url to be splitted in the "application path" (SCRIPT_NAME) and the remaining part (PATH_INFO) - so the webserver detects the application path, and tells the application to handle the remaining part. This allows you to host the same WSGI application on different hostnames and paths, for example you could host it on http://wsgi.example.com/ and http://www.example.com/wsgi/, without the need of configuring your WSGI application for it. 

 So you somehow have to split the url into the required parts, and we use a lua action for this. 
 You can find the source code for it in our repository, source:doc/core.lua (documentation with examples in the source); see "mod_core (lua)"://doc.lighttpd.net/lighttpd2/mod_core.lua.html. (lua)"//doc.lighttpd.net/lighttpd2/mod_core.lua.html. 

 Here the example config for http://xcache.lighttpd.net/ (trac is running as wsgi application) 

 <pre> 
 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; 
 </pre> 

 h2. Spawning your wsgi application 

 Have a look at [[Howto_Rails#spawn-rails-application]] to see how spawning in general can work. For trac the final spawn-fcgi line may look something like this: 
 <pre> 
 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 
 </pre>