Tech Blog

Guide to Ansible/NGINX/uWSGI/Django, Part 2: Getting uWSGI Working with NGINX and Django

Guide to Ansible/NGINX/uWSGI/Django, Part 2: Getting uWSGI Working with NGINX and Django

Recently updated on

In Part 1 of this post, I described the trials, tribulations and ultimate success of running Ansible to provision a cloud server, which we do often here at Imaginary Landscape.  Part 2 concludes the post by describing the process for getting uWSGI working with NGINX and Django.

When I first tested the deployed website after doing the provisioning with Ansible, it failed with a not-very-descriptive "bad gateway" error. Luckily, uWSGI's own documentation provides an excellent tutorial on how to configure uWSGI, NGINX, and Django.

Given the average quality of documentation one usually finds online, it's almost shocking how clear and thorough this tutorial is. It shows you step-by-step how to test each component until you get your Django app working with uWSGI and NGINX. It's great.

The only downside is that because the tutorial is so thorough and because it assumes that you are setting up uWSGI, NGINX and Django from scratch (i.e., not with an Ansible script), working through it takes time.

Nevertheless, when all three of those tools are in play, tracking down the cause of errors can be very difficult. Stripping the system down and checking each component in isolation, as the tutorial describes, might still be the best way forward.

In my case, I eventually figured out that Ansible had created a configuration file for NGINX (site.conf) that contained instructions appropriate for Gunicorn, not uWSGI. Greater familiarity with NGINX configuration files would have helped in this case.

The choice of what kind of configuration to provide should have been controlled by the application_runner variable in extra-variables.yml, which was given a correct value of uwsgi.

On a second attempt, the script got it right. As I write this, I don't know what went wrong the first time. However I have added comments to site.conf that will hopefully provide a clue if this happens again in the future.

Good luck!