Why do I need to use an application server with Django?

Asked

Viewed 196 times

8

Why it is necessary and what is the advantage of using an application server, such as Gunicorn, in a production environment?

It would be possible to use only Django talking directly to the server, such as Nginx?

  • 1

    A push for those who want to write a good answer: https://www.python.org/dev/peps/pep-3333/#original-rationale-and-Goals-from-Pep-333

  • look I am not deep knower of Django I only took courses but during the course Django was presenting as a dev tool not to go live, the same way that happens with the XAMPP or the IIS Express for example, which are tools for the development/testing environment, so live is used for example the Nginx

2 answers

7


In addition to what Kauã replied, a program with uwsgi or Gunicorn has other tasks. Their first function is to communicate with the web server, using the wsgi protocol, as already explained.

The web server cannot run Python directly. In the past, when a web server needed to run a script, it used the Common Gateway Interface (CGI). With CGI, the server passed the request using environment variables and the script generated the response by writing to the console. This worked relatively well in the early days of the web, but calling a script from the web server was not fast, practical, or safe.

In Apache, which supports modules, it was decided to create the mod_python that plugged the interpreter into the web server, following the mod_perl and mod_php model. This coupling solved several problems, but mod_python ran in the same process of the web server, although faster the execution, generated several problems and these solutions were falling into disuse.

Then someone had the idea to create a server that would be responsible for running the scripts. Several protocols emerged, among them WSGI (Web Server Gateway Interface) and Fastcgi (Fast Gateway Interface, widely used with PHP to date). These protocols act as a link between the script (program) and the web server. The advantage is that today you can develop a script that uses the WSGI protocol and plug it into Nginx or apache or any other server that supports WSGI.

Other advantages of the WSGI protocol (used by Gunicorn):

  • You can restart the script if it falls
  • Keeps the program in memory between requests, decreasing boot time
  • You can start multiple copies of scripts to meet multiple requests simultaneously.
  • Supports multiple script types and not just one language
  • Can communicate with scripts using more efficient network protocols such as Unix sockets
  • Optimizes communication using a binary protocol (more compact)

On the side of those who develop the script, the advantage is to be able to count on all this and develop code for a common protocol, independent of the web server. It’s a win-win.

It is very common to run Django with Nginx and gunicorn with multiple processes running Jango. Remembering that in the case of Python, being able to run in multiple processes helps a lot to not have competition problems. With Gunicorn you can also have multiple machines running Django (multiple copies in each), behind a single web server (depending on the system load).

Why do you need an application server? Because Django is not optimized to be a web server. It is not able to respond to multiple connections or scale as a web server (ngnix, apache). A simple web server is included in Django, but is only used for development and in controlled environments, never in production.

More modern versions of uwsgi and guinicorn can also act as web servers, I prefer to have a program specialized in each task that use a replacement, but it all depends on the configuration and system load. A server like ngnix for static files is unbeatable, as well as offering services like redirect, route rewriting, compression, filters, etc.

  • 1

    It is worth detailing that Nginx and apache, when talking to the application server talk to "HTTP" - WSGI and ASGI are the "in process" protocols that these servers use as an interface to run the application code itself. That is: nginxæpache/IIS only does HTTP "forward" (after filter, validation, load-Balancing, serve static content) for application servers - which are HTTP servers by nature.

4

Hello, Thiago! To answer your questions you need to go back a little bit in the history of Python.

Well, come on.

A traditional web server doesn’t understand or can’t run Python applications, then some modules were created for these web Servers (like the Apache mod_python) so that they could run these applications. However, these modules were not a standardized thing, it was just an implementation that allowed Python commands to run on a server, and they were very limited and had many security vulnerabilities.

The community recognized that it was necessary to devise a safe and consistent way to get around these problems of interpreting web Servers, then created the WSGI specification, which in a very simplified way, is a standard and solid interface for running Python code - if you want to go into detail and understand how it works, I recommend taking a look at documentation.

Returning to your question, it is recommended to have a WSGI Server (like Gunicorn) so that this Python execution can happen properly. The main advantages of using them in a production environment are:

  1. Avoids unnecessary processing;
  2. Increases response speed considerably;
  3. Protects your application.

And I haven’t found anything that can make Django talk directly to Nginx. Maybe this direct connection is impossible.

Reunions

  • 2

    The NGINX "talks" http - this is how it communicates with gunicorn and others - the Django demo server is also HTTP - just put in the NGINX configuration that it would connect directly to Django. worth as laboratory and learning - but Jango himself and other smpre framworks warn that these development servers should not be used in Prod.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.