What is the purpose of using nginx with gunicorn?

Question:

I want to use gunicorn for a REST API application with Flask/Python. What is the purpose of adding nginx here to gunicorn? The gunicorn site recommends using gunicorn with nginx.

Asked By: eddys

||

Answers:

In production nginx works as reverse proxy. It means users will hit nginx from browser and nginx will forward the call to your application.
Hope this helps.

Answered By: Asim

Gunicorn is an application server for running your python application instance.

NGINX is a reverse proxy. It accepts incoming connections and decides where they should go next. It is in front of Gunicorn.

Answered By: lostcitizen

Do you know why the Django mascot is a pony? The story is that Django
comes with so many things you want: an ORM, all sorts of middleware,
the admin site…​ “What else do you want, a pony?” Well, Gunicorn
stands for “Green Unicorn” – obeythetestinggoat.com

  • Nginx is the front face for your server.
  • Gunicorn runs multiple django projects(each project is a wsgi
    application powered by Gunicorn) in a single server(say Ubuntu).

Every request comes to nginx and asks for which gunicorn application
should it go and it redirects it.

NOTEGunicorn cannot serve static files automatically as your local django server does. So you will need nginx for that again.

Answered By: subha.py

Nginx has some web server functionality (e.g., serving static pages; SSL handling) that gunicorn does not, whereas gunicorn implements WSGI (which nginx does not).

… Wait, why do we need two servers? Think of Gunicorn as the
application web server that will be running behind nginx – the front-
facing web server. Gunicorn is WSGI-compatible. It can talk to other
applications that support WSGI, like Flask or Django.

Source: https://realpython.com/blog/python/kickstarting-flask-on-ubuntu-setup-and-deployment/

Answered By: eddys

Nginx is a reverse proxy for Gunicorn. Gunicorn serves your Flask app and Nginx sits in front of it and decides where a request should go. For example, if the incoming request is an http request Nginx redirects it to gunicorn, if it is for a static file, it serves it itself. Read more about how to use Nginx and Gunicorn and how to deploy them starting from here.

Answered By: Jahongir Rahmonov
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.