Serving both static and dynamic files with lighttpd + uWSGI.

A common situation: you have one domain and one application, but the app contains both static files (images, js, etc) and dynamic code (e.g. python modules). Lighttpd's configuration is not trivial, and there's an added complication from how uwsgi behaves.

The typical solution for this problem is to use a combination of mod_rewrite and mod_alias, like so:

$HTTP["host"] == "www.server.com"{
       url.rewrite-once = (
              #rewrite /images to /static/images.
               "^(/images.*)$" => "/static/$1",
               #rewrite /js to /static/js
               "^(/js.*)$" => "/static/$1",
               #rewrite everything else to start with /uwsgi
               "^(/.*)$" => "/uwsgi$1",
       )
       #point everything prefixed with /static to a document root
       alias.url = ( "/static" => "/path/to/static/files")
       #set up uwsgi server connection
       uwsgi.server = (
               "/uwsgi" => (( "host" => "127.0.0.1", "port" => 3033, ))
       )
}

However, if you were used to having uwsgi proxy to your_wsgi_module.application, you'll suddenly get errors:

uWSGI Error
application not found

This is because if the uwsgi.server configuration specifies a name ("/uwsgi"), it expects to find the application in your_wsgi_module.applications[name]. Simple fix in your_wsgi_module:

from myapp import MyApp, SomeMiddleWare
application = MyApp()
application = SomeMiddleWare(MyApp)

applications = {"/uwsgi":application}

Incidentally, make sure you are using the latest mod_uwsgi.c; now that people are using it, a lot of bugs are becoming exposed and getting fixed.


Werkzeug DebuggedApplication with uWSGI under lighttpd.

Thu 08 July 2010 by George Dorn

uWSGI is a fairly awesome app server; it's lightweight and fast, yet highly stable.

Unfortunately, mod_uwsgi for lighttpd is somewhat half-baked. Aside from yet another app server using the equivalent of urllib.unquote instead of urllib.unquote_plus, it also behaves differently than most app servers in dealing with query ...

read more