Django, Uwsgi & Nginx: 3 Vital Keys to Bold Website Building

Feeling OK
Linode Cube
Published in
5 min readSep 8, 2016

Plenty of applications today are served with a Django, uwsgi and nginx stack. This setup, plus a database, is all that’s needed to make an app accessible to the outside world. For many, that’s enough to know that the app is available, which is all that’s really important.

For me, however, recommendations aren’t enough to make me want to use something. Nor is recognizing the underlying stack. I want to know how its components can render such a powerful stack. Just like knowing what’s in your food, the more knowledge you have gives you greater control over your decisions and ultimately, your actions.

So, here’s the app availability “nutrition” behind Django, uwsgi and nginx.

Django

Django is open-source and free. When inquiring about it, you’ve probably heard this: “Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.” This really does describe Django in a nutshell, but let’s break it down further.

A web framework is useful for developing content as it provides shortcuts to get things done, eliminating certain design headaches and saving time. Django is a web framework for Python, one of the most popular programming languages used today.

Django is made by users who are familiar with Python and the “shortcut” they’re integrating into the framework; you can trust that any accepted push has been peer reviewed and time tested. So, sure, you could write every part of any and every page in html, but if you could have most of it done already and then make the tweaks where you need, why not do so and save time?

Django is powerful, especially since it is seen as a Model-View-Controller (MVC) architecture. This will separate the code-defining and access data from the request routing portion, which is also separate from the user interface.

Each part of a web application powered by Django has its own purpose. This means that a single part needing to be tweaked will not require you to go through all preceding code to get to that part. You can change the portion needed, and as long as your code checks out, you’re good to go.

There’s no real trick to learning Django. It’s really just throwing yourself in and getting accustomed to the best practices; exposure leads to understanding what can be done and how.

Because Django is a framework, it’s really a library with much of the annoying and often visited actions already done for you. Finding out what’s at your fingertips takes time, but the Django community certainly makes it easy to settle in.

Uwsgi

Web servers serve content, speaking directly to the outside world, but most don’t speak directly to Django applications. This task would be left to something that would, such as uwsgi, which is both a protocol and an application server.

(Uwsgi can be confused with wsgi, but they’re quite different as wsgi is a protocol only)

Uwsgi is fast. Uwsgi is versatile. It introduces very little overhead in whatever stack it’s used in and can accept plugins, working with nearly any kind of server setup. Uwsgi plugins are written in C, C++, and Objective C, so knowledge in any of these languages can lead to personalized plugins for a specific user’s needs.

Uwsgi has a mode called “Emperor mode” that helps a user deploy a large number of apps on a single server. This mode will scan through the server it resides on, looking for uwsgi-compatible configuration files. Once such files are found, a new instance of uwsgi is created and will manage them, performing actions such as starting and stopping them.

These instances are called vassals, and all vassals are maintained by the emperor. If a vassal dies unexpectedly, the emperor will respawn it, but if the emperor dies, all vassals die.

Emperor mode is very powerful. It allows for effective managing of multiple applications in one place, but does so with effective vassal management features, including:

  • Identifying when a vassal is doing its job or not (successful vassals are considered ‘loyal’)
  • Throttling whenever multiple vassals are created in the same second, preventing fork bombing
  • Blacklisting any vassals not performing well and throttling their actions
  • Manually enabled heartbeat checking, and if a heartbeat fails the vassal will be respawned

For more information in uwsgi, I recommend checking out their project page.

Nginx

Because I’ve covered nginx in a recent blog, I’ll only give a brief overview here.

Nginx is one of the most popular and powerful web servers used today. It’s usable by a large number of unix-based systems, as well as Windows and BSD operating systems. Known for its lightweight footprint, it serves static pages at surprisingly fast speeds. It is event-driven, so it can handle large loads of requests in quick, respectable times while consuming few resources.

Nginx also fulfills other roles, such as being a load balancer for a server, routing traffic to multiple backends. Another great power it has is caching, further reducing the strain on CPU by dealing the same cached content for repeated requests of said content, not needing to hit the database every time.

Both load balancing and caching allow nginx to work with other web servers very well, effectively handling all traffic intelligently, routing the content to where it needs to go, and serving cached content for other web servers simultaneously.

Oh, and it’s open-source and free.

How it all works together

Django, is not designed to speak with outside traffic directly, so you need uwsgi to do that. Though uwsgi is able to be used as a standalone frontend for a server, it works exponentially better when used with a service handling the traffic in its stead.

This is one of the reasons why nginx is often installed along with uwsgi. This allows caching to take place and reduces excessive strain that otherwise would’ve pummelled the server. With the powers of nginx up front, a server is able to intelligently send all new queries to uwsgi while it handles that static and cached content.

So, let’s look at this from beginning to end:

Just hearing “this stack is what everyone uses” isn’t enough for me; I want to know why each part of my stack is useful.

It’s easy to see the usefulness beneath the Django/uwsgi/nginx stack:

  • Django is the a powerful framework,
  • Uwsgi is an effective means to manage the applications created
  • Nginx effectively and efficiently deals with the traffic-requesting the application.

I hope this post helps some of you guys better understand this stack as much as it helps me.

If you need some instructions on how to install all three, Linode has a guide detailing each step. If you found this information helpful, please share it.

--

--

Feeling OK
Linode Cube

I'm feeling alright Customer Advocate for https://www.linode.com/, gamer, eater of vegetables (but I hate them!)