By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

Django hosting on Nginx with uwsgi for high performance

Nginx:

Nginx is a high-performance HTTP server, acts as a reverse proxy. Nginx is open source. Nginx is prominent for its high performance, load balancing and less resource utilization.

Why Nginx is fast and uses less resources:

Because Nginx is event driven, unlike apache where for each session, it creates a different thread these threads gets blocked whenever I/O needs to be performed (e.g. from network). Since a web server typically handles ten of thousands of concurrent sessions, a large number of threads are required. This leads to high overhead and wastage of CPU cycles in doing thread context switches. Nginx avoids the high overhead of context switches by keeping a number of threads limited and thus make efficient use of CPU cycles. Hence it is more efficient than apache.

Lets start configuring uwsgin and nginx for django:

Here we assume that you have already has a working django application. Let the application name be 'hodor' and the path be '/home/hodor/hodo'

1. uwsgi configuration:

    Create a hodor_uwsgi.ini file which contains uwsgi configuration:

[uwsgi]

chdir           = /home/hodor/hodor
module          = hodor.wsgi
# the virtualenv (full path)
home            = /home/hodor/env

# process-related settings
master          = true
processes       = 8
socket          = /home/hodor/hodor.sock
chmod-socket    = 666
max-requests    = 50000

The file is self explanatory, when we run the uwsgi the above config creates a hodor.sock file which inturn is used by nginx for communication.

Before using Nginx, let's test our uwsgi configuration, so run:

uwsgi --ini hodor_uwsig.ini

Now, this should create a hodor.sock file and also you should be able to get your site running.

2. Nginx configuration:

     Nginx configurations are meant to be place in /etc/nginx/sites-available/ directory. So let's create a hodor.config file in that directory with following contents:

upstream hodor {
    server unix:///home/hodor/hodor.sock;
}

server {
    listen      80;
    server_name your_domain_or_ip;
    charset     utf-8;

    client_max_body_size 1024M;

    location /static {
        alias /home/hodor/hodor/static;
    }

    location /media {
        alias /home/hodor/hodor/media;
    }

    location / {
        uwsgi_pass mt;
        include     uwsgi_params;
    }


    access_log /var/log/nginx/hodor_access.log;
    error_log  /var/log/nginx/hodor_error.log  warn;
}

upstream means all requests for / go to the any of the servers listed under upstream hodor, with a preference for port 80. It is commonly used for a cluster for load balancing. Listen specifies the port on which to listen, server_name is your domain or IP addr. location /static specifies that when the URL requests for a static page then the location path(the alias path) is used to server the request. access and error logs path can be edited as required.

Now enable the configuration by linking it to a sites-enabled directory, run:

ln -s /etc/nginx/sites-available/hodor.conf /etc/nginx/sites-enabled/

Test Nginx for configuration errors, run:

nginx -t

Restart Nginx

service nginx restart
    Posted On
  • 04 October 2013
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to setup http password authentication with nginx

HTTP Authentication is used to allow access limit to a site or particular directories by validating the username and password. HTTP basic authentication can also ...

Continue Reading...
Setting Up Sentry - Web Application Event Tracking platform

When you want to track your exception and log mesages in a UI rather than storing it in a file(which we usually do), we can ...

Continue Reading...
How to backup and restore mysql, postgresql and mongodb databases

Data loss can happen when we accidentally delete the files, or when server crashes or system fails, or when we applied migrations to the data ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties