Ben Godfrey

Nginx+Django+FastCGI

A lot of people seem to have posts like this, but there were some things that I got stuck on when moving to Nginx from Apache.

location ^~

The ^~ match can not be used with regular expressions.

This will not work as expected:

location ^~ /(foo|bar)/ {
    ...
}

Use the ~ match operator if you want to use an RE. Make sure that your RE matches don’t clash with you plain string matches. The latter will be preferred in this event.

location + root

If you want to specify a different root within a location block, be mindful that the uri is unchanged.

For example, if you want to publish Django’s admin media, you might write something like this:

location ^~ /admin/media/ {
    root /usr/local/django/django/contrib/admin/media;
}

When a request comes in, Nginx will concatenate the root and the uri to find the file to server. With this config, it will try to serve /usr/local/django/django/contrib/admin/media/admin/media/css/base.css.

SCRIPT_NAME and PATH_INFO

Django uses PATH_INFO to match against urlpatterns. Nginx’s fastcgi_params include doesn’t set that. It does set SCRIPT_NAME. If both PATH_INFO and SCRIPT_NAME are set to $fastcgi_script_name, Django seems to get an empty path for all requests. Just set PATH_INFO!

Request buffering to file

Nginx buffers large requests to file before passing them to an upstream server. There is no option to stop this from happening. If you want to track the progress of request uploads, you will need to use the Upload Progress Module.

Comments

mat's avatar

mat

oooh, nginx. how xciting. I haven't tried it, but have heard it spoken very well of.. Almost did, the other day (for a teeny-tiny php torrent server, in-house), but went with lighty+fastcgi in the end. Little, fast, webservers are teh ace.
Cancel

Comments are closed for this post.

Ben Godfrey http://aftnn.org

I don't think there's much in it between Nginx and Lighty. Lighty is memory lighter on a very small install (Nginx starts at least 2 processes, Lighty can cope with 1).

The app I was building accepts uploads and not much else. I managed to push almost the entire app into static stuff. The actual server app work is very minimal. This makes me happy.
Cancel

Comments are closed for this post.

jehe.pip.verisignlabs.com/'s avatar

jehe

Helpful tips. Thanks!

-Jesse

Cancel

Comments are closed for this post.

xocekim+aftnn-gmail.com's avatar

Mike

Spent a long time working this out tonight. My Django with Nginx was just going to index page no-matter what the url path was. Fixed by removing SCRIPT_NAME from the fastcgi.conf that comes standard with Nginx 1.0.1.

Thanks!

Cancel

Comments are closed for this post.

Add a new comment

Comments are closed for this post.