Table Of Contents

Previous topic

Running TurboGears Behind Apache

Next topic

Deployment behind Apache 2 + mod_python

Running TurboGears behind Nginx

Nginx is a fast and light HTTP server, reverse proxy, load balancer (and more).

Using Nginx as a Reverse Proxy

It’s pretty simple to get TurboGears set up behind a Nginx server so that it proxies requests to the CherryPy server. Here is a sample configuration that not only proxies to your TurboGears application, but serves static content with Nginx and load balances between two TurboGears application instances as well.

http {
    # boilerplate nginx config ...

    upstream mycluster {

    server {
        listen 80;

        # static files
        location ^~ /static/  {
            root /path/to/YourProject/package;
        location = /favicon.ico  {
            root /path/to/YourProject/package/static/images;

        # proxy to turbogears app
        location / {
            proxy_pass          http://mycluster;
            proxy_redirect      off;
            proxy_set_header    Host $host;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

Next you need to setup a couple TurboGears backends that will comprise the cluster:

Edit your production configuration (e.g. prod.cfg) and add/change the following lines:


Copy prod.cfg to prod2.cfg and change the server.socket_port option:


Start both instances of your app:

$ python prod.cfg &
$ python prod2.cfg &

That’s it! Nginx should now be passing requests across both backends transparently.


You can find more information and recipes for setting up Nginx on the Nginx wiki.