Nginx + Django + FastCGI + SSL

My favourite method for deploying Django sites in production is with Nginx at the frontend talking FastCGI to a dedicated Django process (either flup or supervisord) via a UNIX or TCP socket.

There are two recurring issues that I keep running into costing hours of fruitless Googling. Hopefully, this will help others:

  • SCRIPT_NAME is evil! At least it’s useless in Django, and if present as a FastCGI parameter, Django will trip that away from your request URI and you’re almost certainly going to get / no matter what URI you navigate to! So, save SCRIPT_NAME for your PHP stuff, and leave it out of the parts where you expect to be talking to Django.
    If you have include fastcgi_params in your nginx.conf, make sure it’s not hiding behind the fastcgi_params file.
  • If you want SSL to work, i.e. for Django to know that it is serving in https mode, and to build proper Location header for redirects, you need to include the following in the appropriate section of your nginx.conf:

    fastcgi_param HTTPS on;

    Obviously, if you’re running both plain and SSL version, you shouldn’t be sharing that section or Django will just thinking that it is in SSL mode even when it’s not.

3 Responses to “Nginx + Django + FastCGI + SSL”

  1. Tweets that mention Wil Tan » Blog Archive » Nginx + Django + FastCGI + SSL -- Says:

    […] This post was mentioned on Twitter by Django Ireland, Wil Tan. Wil Tan said: Nginx + Django + FastCGI + SSL […]

  2. Chad Says:

    Thanks for that HTTPS on tip!

  3. IGLOO Says:

    +1 on HTTPS fastcgi param!