#19 Django in Production - From Zero to Hero - Part-2

In this lesson we continue deployment process of Django based application. In this part we deal with Nginx.

In previous lesson we prepared our VPS. In this lesson we take care of web server part. The application who first meets, so to speak, your web requests is the web server and not Django application. There are couple of reasons for that. First of all, Django application by design is really great at working with complex business logic, interacting with database, managing users and authentication and so on. But when it comes for example to handle TLS (secure HTTP) layer - Django Web Framework just says "it is not my problem". Same with static files - css, javascript files, images, fonts; dealing with static files is just out of scope for Django Framework.

Yes, Django Web Framework can serve static files. But that is only out of courtesy to ease your web application development process. Long story short - you need a real web server in production. Top 2 choices are Apache and Nginx. In this lesson we use Nginx.

Hello, Http Rquest!

Before jumping on how to install and configure Nginx, let's understand it first. Web servers, Nginx in particular, usually hosts more than one web application (or website). So, one of the very first thing webserver needs to do, is to decide to whom that http request is addressed. In picture below NginX hosts 3 applications demo.dglte.net, papermerge.com and django-lessons.com. Each of those application has it's own requirements and specifics, for example papermerge.com is just a bunch of html and css files, while both django-lessons.com and demo.dglte.net are full fledged Django Applications. From the point of Nginx each of those 3 things are called - virtual server.


In practice, each virtual server is usually a separate file stored in directory /etc/nginx/sites-available/. So a name like 'site available' instead of virtual server is less scary.

To configure Nginx to serve demo.dglte.com, you need to create file /etc/nginx/sites-available/demo.dglte.com - with following content:

server {
    listen 80 ;
    server_name demo.dglte.net;

    location /static/ {
        alias /opt/demo/static/;

    location / {
       proxy_pass http://localhost:8000;


How you name the file, is not important, I chose to name it demo.dglte.net because it describes files purpose perfectly. It is not even important where you place the configuration file for that site. What is important though is to create a symbolic link to it from /etc/nginx/sites-enabled/:

cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/site-available/demo.dglte.net    # with ln -s command if you specify only the target

In nutshell, /etc/nginx/sites-available/demo.dglte.net defines a server directive, which instructs Nginx how to serve all incoming http requests from domain specified by server_name, on port specified by directive listen. Each server directive will have only one server_name. Another very important directive is alias directive.

Alias directive works by replacing the matched argument with its argument. See picture below for examples:


Result in the figure above is a file on file system which will be served by Nginx.

During screencast I use dig command to troubleshoot DNS related issue. Dig command has following format:

dig <domain> -t <DNS Record Type>

DNS record for mapping to IP address is A Record, I think A stands for alias.

Another popular command line tool for troubleshooting DNS is drill

Wow, no similar lessons found! You have just found a very unique lesson!