Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What benefit is added by using Gunicorn + Nginx + Flask? [duplicate]

I see people are running setups like Nginx + Gunicorn + Flask.

Can someone explain what is the benefit of having Gunicorn in front of Flask? Why not just run Flask alone? Doesn't it consume more resources having Gunicorn + Flask running? Is Gunicorn able to reboot the Flask instance when it fails to respond?

What's also the purpose of having nginx on top of gunicorn? Isn't gunicorn enough? Again, more resources being spent?

like image 311
KJW Avatar asked Dec 24 '13 21:12

KJW


People also ask

What is the benefit of Gunicorn?

Gunicorns takes care of running multiple instances of your web application, making sure they are healthy and restart them as needed, distributing incoming requests across those instances and communicate with the web server. In addition to that, Gunicorn is pretty darn fast about it.

Why do we need nginx with Gunicorn?

Nginx and Gunicorn work togetherGunicorn translates requests which it gets from Nginx into a format which your web application can handle, and makes sure that your code is executed when needed. They make a great team! Each can do something, which the other can't.

Why do we need Gunicorn with flask?

Gunicorn is a Python WSGI HTTP Server that uses a pre-fork worker model. By using gunicorn, you'll be able to serve your Flask application on more than one thread.


1 Answers

I think you may be confused, Flask is not a web server, it is a framework and needs some sort of web server, such as Gunicorn, Nginx or Apache, to accept HTTP requests which it will then operate on. The reason why people run Nginx and Gunicorn together is that in addition to being a web server, Nginx can also proxy connections to Gunicorn which brings certain performance benefits, here is a pretty good answer that elaborates on those benefits: https://serverfault.com/questions/220046/why-is-setting-nginx-as-a-reverse-proxy-a-good-idea

EDIT: Added link containing information about performance benefits of running Nginx as a proxy.

like image 84
Jon Avatar answered Sep 19 '22 18:09

Jon