Web Server and Application Server in Django
As explained in the beginning of this chapter, two types of server functionalities are required for dynamic web applications – a web server and application server. The key difference between those two servers is that a web server manages static content while an application handles dynamic content.
The main figure illustrates a use case in Django applications using Nginx as a web server and Gunicorn as an application server. As Nginx handles all requests from clients first, it is also called a reverse proxy server.
The mechanism is as follows:
- Using the
collectstatic
command, all static files are stored in a particular location that Nginx can also recognize. (This is important for Nginx configurations. We'll explain this later.) - When an HTTP request for static assets comes, Nginx handles the request directly. The static URL is defined in the settings.py (
STATIC_URL
). - When an HTTP request is not for static assets (request for dynamic content), the request goes through Gunicorn, and the Django app handles the request using the URL dispatcher, Views and Models, etc.