What is the purpose of NGINX and Gunicorn running in parallel?

DjangoNginxGunicorn

Django Problem Overview


A lot of Django app deployments over Amazon's EC2 use HTTP servers NGINX and Gunicorn.

I was wondering what they actually do and why both are used in parallel. What is the purpose of running them both in parallel?

Django Solutions


Solution 1 - Django

They aren't used in parallel. NGINX is a reverse proxy. It's first in line. It accepts incoming connections and decides where they should go next. It also (usually) serves static media such as CSS, JS and images. It can also do other things such as encryption via SSL, caching etc.

Gunicorn is the next layer and is an application server. NGINX sees that the incoming connection is for www.domain.com and knows (via configuration files) that it should pass that connection onto Gunicorn. Gunicorn is a WSGI server which is basically a:

> simple and universal interface between web servers and web applications or frameworks

Gunicorn's job is to manage and run the Django instance(s) (similar to using django-admin runserver during development)

The contrast to this setup is to use Apache with the mod_wsgi module. In this situation, the application server is actually a part of Apache, running as a module.

Solution 2 - Django

Nginx and Gunicorn are not used in parrallel.

  • Gunicorn, is a Web Server Gateway Interface (WSGI) server implementation that is commonly used to run Python web applications.
  • NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.
  • Nginx responsible for serving static content, gzip compression, ssl, proxy_buffers and other HTTP stuff.While gunicorn is a Python HTTP server that interfaces with both nginx and your actual python web-app code to serve dynamic content.

The following diagrams shows how nginx and Gunicorn interact.

Nginx and GunicornOverall idea of nginx and Gunicorn

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeadlockView Question on Stackoverflow
Solution 1 - DjangoTimmy O'MahonyView Answer on Stackoverflow
Solution 2 - DjangoThusitha DeepalView Answer on Stackoverflow