in Hacks

Setting up nginx with Bottle and Flup (FastCGI)

Writing the bottle app

Again, two things are important here.

  1. We got to have the routing correct
  2. We have to set the server = “flup”

Using flup

What is flup? Flup is a random assortment of WSGI server and most importantly for us, fcgi. You can install it with pip:

Now we can run our application with flup.

Installing and configuring nginx

If you’re running a Debian-based distribution it’s just:

Start by making a backup copy of the nginx.conf which is in /etc/nginx/:

Now we can edit it and connect it to our bottle application.

Setting nginx up for fast cgi

The basic structure in nginx’s config files is:

in server {} comes everything related to the servers, e.g. ports and host names. In location /foobar {} you insert everything to related a location – in this case /foobar.

Configuring a server

We start with the following code:

This creates a server which runs on port 8080. Furthermore, we apply rules to two locations. First / to which we assign /home/foobar/www. That means that every request at / will request files / paths in /home/foobar/www. Some examples:

However, we defined an exception: /app/. We want to use fastcgi in this case. nginx offers this ability natively. You just write the following:

It’s a lot of lines but the first one is the one you have to edit. It says that fastcgi requests should be forwarded to localhost:8889 which, in this case, is our bottle application. You can put the rest into an extra file and use include file to include it.

Starting nginx and bottle

First, we want to start our nginx server:

If that runs, we just start our bottle app and we’re done.

Write a Comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.