Code


Version 13 (modified by CollinAnderson, 2 years ago) (diff)

rewrote most of the page. previous instructions were from 2008

nginx is a reverse proxy and needs to connect to a server that contains django. This guide is for using fastcgi, but you could instead use apache+mod_wsgi, gunicorn, uwsgi, or SCGI+Cherokee


This assumes you are using ubuntu 10.04 or later.

You'll need nginx and flup installed.

sudo aptitude install nginx python-flup

In your django project, start up fastcgi. See the fastcgi docs for more info.

python ./manage.py runfcgi host=127.0.0.1 port=8080

Next, we need to create a configuration file in /etc/nginx/sites-available/sample_project.conf and symlink it to /etc/nginx/sites-enabled/sample_project.conf:

sudo touch /etc/nginx/sites-available/sample_project.conf
sudo ln -s /etc/nginx/sites-available/sample_project.conf /etc/nginx/sites-enabled/sample_project.conf

Here is what a minimal conf file would look like:

server {
    listen 80;
    server_name myhostname.com;
    access_log /var/log/nginx/sample_project.access.log;
    error_log /var/log/nginx/sample_project.error.log;

    # https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production
    location /static/ { # STATIC_URL
        alias /home/www/myhostname.com/static/; # STATIC_ROOT
        expires 30d;
    }

    location /media/ { # MEDIA_URL
        alias /home/www/myhostname/static/; # MEDIA_ROOT
        expires 30d;
    }

    location / {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:8080;
    }
}

Now, reload nginx.

sudo /etc/init.d/nginx reload

You may use a unix socket instead of a port number like so: fastcgi_pass unix:/home/www/myhostname/fastcgi.sock

You'll probably want to keep your fastcgi process running using upstart, init.d, or supervisor

See also: