Writing the bottle app
from bottle import route, run @route('/app/') def hello_name(name): return "Hello " + name run(server="flup" host='localhost', port=8080)
Again, two things are important here.
- We got to have the routing correct
- 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:
pip install flup
Now we can run our application with flup.
Installing and configuring nginx
If you’re running a Debian-based distribution it’s just:
sudo apt-get install nginx
Start by making a backup copy of the nginx.conf
which is in /etc/nginx/
:
cp /etc/nginx/nginx.conf .
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:
http { server { location { } }
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:
server { listen 8080; location / { root /home/foobar/www; } location /app/ { # magic } }
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:
http://location:8080/ -> /home/foobar/www http://location:8080/test -> /home/foobar/www/test http://location:8080/test.html -> /home/foobar/www/test.html http://location:8080/some/directory/baz.html -> /home/foobar/www/some/directory/baz.html
However, we defined an exception: /app/. We want to use fastcgi in this case. nginx offers this ability natively. You just write the following:
location /queue/ { fastcgi_pass localhost:8889; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; }
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:
sudo nginx # or if we want to use a special config file sudo nginx -c /path/to/nginx.conf
If that runs, we just start our bottle app and we’re done.
python bottle.py