Nginx is a high performance HTTP server, acts as a reverse proxy. Nginx is opensource. 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 webserver 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 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:
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, lets test our uwsgi configuration, so run:
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 lets create a hodor.config file in that directory with following contents:
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 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 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 sites-enabled directory, run:
Test nginx for configuration errors, run: