Deploying circuits.web with Nginx/uwsgi

Configuring Nginx

Now we’re going to setup the configuration a little bit. Assuming that you have web files in your usual html folder, for example html/

# uwsgi --version

This is basically what we have going on, in your document folder:

server {
	location / { 
		#You can also use sockets such as : uwsgi_pass unix:/tmp/uwsgi.sock;
		include uwsgi_params;

You’ll notice that Nginx has been set to the public folder as the website’s root, but for the backend we have set the UWSGI_CHDIR variable to the root where the python app exists, and set UWSGI_SCRIPT to the name of the script not including the .py extension. Essentially getting ready to try dynamic applications with uWSGI.

The above will try the request against try_files, and if it is an existing file or folder within the /public folder it will serve it. You may have noticed the error_pages line, since / is a valid folder, nginx may attempt to serve it as such, but if there’s no index file in /public it may cause a 403 or 404 error, we want to invoke the backend if this happens, so we revert the code to 200 and send it on it’s way to @backend.

Now lets create the circuits.web demonstration script and save it as (I’ll explain what each part does on the next page).

def application(environ, start_response):
    start_response("200 OK", [("Content-Type", "text/plain")])
    return ["Hello World!"]

And now to create a configuration file to be used with uWSGI this will make it easier to start it up in the future:
(Do not copy the <— comments they’re just there for explanation)

# uwsgi -s -w myapp
*** Starting uWSGI (64bit) on [Mon Jan 31 00:10:36 2011] ***
compiled with version: 4.4.5
Python version: 2.6.6 (r266:84292, Dec 26 2010, 22:48:11)  [GCC 4.4.5]
uWSGI running as root, you can use --uid/--gid/--chroot options
 *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
 *** WARNING: you are running uWSGI without its master process manager ***
your memory page size is 4096 bytes
allocated 640 bytes (0 KB) for 1 request's buffer.
binding on TCP port: 3031
your server socket listen backlog is limited to 100 connections
initializing hooks...done.
...getting the applications list from the 'myapp' module...
uwsgi.applications dictionary is not defined, trying with the "applications" one...
applications dictionary is not defined, trying with the "application" callable.
application 0 () ready
setting default application to 0
spawned uWSGI worker 1 (and the only) (pid: 20317)

Now that you have the above configuration you can save it as something like uwsgi.xml, essentially this will cause uWSGI to launch process(es) into the background, with a self-healing master that can respawn workers if needed. You’ll notice we did not specify a python file to be served, thats because it’s being taken care of in the Nginx configuration now.

To launch the above we would run:

apt-get install python-setuptools

Make sure to restart/reload Nginx at this point (also stop/restart uWSGI if you make any changes to your apps)

When you visit your site at you should see a simple “Hello World!”

Onto page 3 where I explain what’s going on.

Comments are closed.