68

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?

B--rian
  • 5,578
  • 10
  • 38
  • 89
KJW
  • 15,035
  • 47
  • 137
  • 243
  • 1
    Gunicorn is a Python WSGI HTTP Server that usually lives between a reverse proxy (e.g., Nginx) or a load balancer (e.g., AWS ELB) and a web application such as Django or Flask. good article: https://medium.com/building-the-system/gunicorn-3-means-of-concurrency-efbb547674b7 – Anant Dec 12 '19 at 22:01
  • See this for more - https://serverfault.com/a/331263/564406 – Amaimersion Mar 17 '20 at 15:26
  • And this - https://vsupalov.com/gunicorn-and-nginx/ – Amaimersion Mar 17 '20 at 18:01

1 Answers1

73

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.

Community
  • 1
  • 1
Jon
  • 1,122
  • 8
  • 10
  • 6
    If the Flask it self is not a web server, does it come shipped with some basic web server so that we can start it and access it via HTTP (default port 5000), [docs](http://flask.pocoo.org/docs/0.10/api/#flask.Flask.run)? Although at the beginning they mention "The flask object implements a WSGI application", can WSGI applications talk HTTP directly and optionally via a proxy pass from a web server? [This](http://www.fullstackpython.com/wsgi-servers.html) talks about WSGI servers... – NikoNyrh Sep 30 '15 at 07:56
  • 8
    Ahaa, at [Deployment Options](http://flask.pocoo.org/docs/0.10/deploying/) they mention "You can use the builtin server during development, but you should use a full deployment option for production applications". – NikoNyrh Sep 30 '15 at 08:17
  • 5
    Yes, flask by default uses the Werkzeug wsgi server, altough not as efficient as the Gunicorn server, it is easily a good testing and staging server. – ffleandro Nov 01 '17 at 22:11
  • This answer still pop up high in search, so here's an updated [Deployment Options](https://flask.palletsprojects.com/en/1.1.x/tutorial/deploy/) link from NikoNyrh's comment. – shayaan Jun 25 '20 at 00:18