Nginx & FastCGI/WSGI Django deployment

magical powers

Monday, September 29th, 2008

Reverse proxying application server (FastCGI / WSGI / Apache) through Nginx is my favourite deployment setup for Django web application framework.
I reviewed my options before settling on this setup that I think it is is the best for my deployments.

How to?

  1. Install Nginx
  2. Configure Nginx (see configuration file below)
  3. Make sure your backend server runs otherwise you’ll be welcomed by an error message 512 bad gateway, e.g. start fastcgi with manage.py

Nginx configuration file

Tested on Ubuntu Linux 8.04

worker_processes 1;
error_log logs/error.log info;
pid /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include mime.types;
    default_type application/octet-stream;

    server {
        listen 80;
        server_name localhost;
        location / {
            # Launch proxied server on a separate port
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-Host $server_name;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        location /media/  {
            # Symlink to django admin media files
            alias /source/media/;
        }
        location /static/ {
            # static files for the project
            root  /clients/goto;
        }
    }
}

To add a new site you need to add another Nginx server{} to your config file and run another instance of the backend server.

You can use reverse proxy with Apache/mod_python, FastCGI or anything else. I’ve used this with the WSGI server extracted from the CherryPy and it works like charm.

This is also my preffered Django development setup. The development server is much faster if it doesn’t have to serve static files. Just proxy Nginx to the development server.

5 Responses to “Nginx & FastCGI/WSGI Django deployment”

  1. The title also refers to WSGI, presumably with the nginx WSGI add-on http://wiki.codemongers.com/NginxNgxWSGIModule.

    Have you tested with nginx WSGI, and is the recommended configuration similar?

    Jeff Kowalczyk September 29th, 2008 at 6:34 pm
  2. The heading should perhaps read “Proxy Django application server through Nginx” as it doesn’t really matter what is the backend server.
    Nginx will take care of serving static files and load balancing if you add more application server instances.
    No, I haven’t used Nginx WSGI module and I wouldn’t want to use it. Nginx is proven as a proxy and good to serve static files. If you hate flup, grab your WSGI server from CherryPy and you can upstream to as many CherryPy WSGI server instances as you wish. This would be very similar to Rails setup with upstream Mongrels.

    Frank Malina September 29th, 2008 at 7:00 pm
  3. I’ve been using a similar setup for quite some time now and can recommend it.

    Prairie Dogg September 29th, 2008 at 7:38 pm
  4. Do you use a process watcher of some sort to restart the FastCGI instance for each site?

    Kip September 30th, 2008 at 9:03 am
  5. Monit

    Frank Malina September 30th, 2008 at 10:16 am

So what do you think?





Let's do business. Skype "vizualbod" or e-mail to vizualbod@vizualbod.com.

Slovensky »