0

I've found few articles on the Internet, discussing the integration of heavy-duty web server like "Nginx" to the Flask applications, but along with Nginx, they are also taking the help of "Gunicorn" Web server.

My question is that, Why is it necessary to use "Gunicorn" web server along with uWSGI or Nginx?

2 Answers 2

5

You need to understand what is Web Server Gateway Interface (WSGI) to understand the difference between Gunicorn, uWSGI and Nginx.

What does it mean?

[WSGI is] a proposed standard interface between web servers and Python web applications or frameworks, to promote web application portability across a variety of web servers. Source: PEP 333

As explained above, it is a standardization that simplifies the implementation of a Web application in python within a server. WSGI describes how the interactions between a server and a python application must be. No matter what your application is, if you follow the rules defined in PEP 333, then any WSGI-compatible HTTP server will be able to communicate with your application.

But there is no point in reinventing the wheel, developers have already created WSGI applications so that you can create applications more easily. Flask is one of them, but there are others.

Why do I need WSGI HTTP server? I can run my Flask application in the cmd, isn't it?

You always need a WSGI HTTP server. Flask is a very good tool and it directly includes a Werkzeug development server but you must not use Werkzeug server in production.

The [Werkzeug] development server is not intended to be used on production systems. It was designed especially for development purposes and performs poorly under high load. Source: Werkzeug Documentation

That is why you always have this message when you try to run your flask application with the command run().

WARNING: Do not use the development server in a production environment.

Then you will need a server for production.

Gunicorn, uWSGI, Twisted Web etc?

There are many WSGI-compatible servers available. The choice is purely arbitrary and depends exclusively on your needs. The installation and configuration can be different. More or fewer options will be available. It's up to you to find out which one suits you best. There is a part of the Flask documentation that is reserved for this: Standalone WSGI Container.

This link could be useful too : Flask Deploying

What is Nginx's role?

Nginx can be used as a reverse proxy.

A reverse proxy is an intermediary proxy service which takes a client request, passes it on to one or more servers, and subsequently delivers the server's response to the client. Source: Setting up an Nginx Reverse Proxy

To Go Further

What is the advantage of using Nginx + WSGI server? CI/CD.

It is really easy to dockerize an Nginx server and your application within a WSGI server. Then you can manage your dockers with Kubernetes and manage the continuous integration with Jenkins. DevOps Culture.

However, this is absolutely not mandatory. A solution that is also very nice to use is Nginx Unit, it allows you to have the power of Nginx and to part with Gunicorn or uWSGI. The flaw that could be found in this solution is that it is not compatible with the Docker + Kubernetes + Jenkins fashion that you can find easily on Google. It is quite possible to do continuous integration on it, but it is less common. This will require more knowledge: blue/green environment, etc....

1
  • Got it, Thanks :) Jul 19, 2019 at 13:58
0

You can follow these blogs to deploy by connecting to Nginx

Without the use of Gunicorn, the process becomes complicated with uwsgi

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04

https://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/

1
  • Thanks, But why they used Gunicorn along with those servers? There should be some reason. Jul 19, 2019 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.