Nginx, Gunicorn, Flask Difference Comparison

Role division

Flask (Django):Python Web applications (such as Flask, Django) nginx: Web server, From the user’s point of view, the request-response Gateway WSGI (Web Server Gateway Interface), translated as Python Web Server Gateway Interface, is directly used for docking. A communication protocol between a Python Web application (such as Flask) and a Web server (such as Nginx). In other words, if you want your Web application to run on any server, you must follow this protocol.

Several deployment methods differ

Flask built-in WebServer + Flask App = weak chicken version of the Server, Single process (single worker)/failed to die/not easy Scale Gunicorn + Flask App = multi-process (multi-worker)/multi-threading/failure automatically help you restart the worker/simple Scale multi-nginx + multi- Gunicorn + Flask App = small multi-instance Web application. Gunicorn will also hang its supervisor

Why gunicorn

Simply put, the server that comes with Flask is too weak (it takes a few requests to fill up). 2 major defects

The "single Worker" has only one process running all requests, and the built-in webserver can easily get stuck due to the poor implementation. And there is only one Worker running request. Under multicore CPUs, only one core is used. Of course, you can do more than one process. - "Lack of Worker management" is added, and Gunicorn can adjust the number of workers. And this thing, the built-in Webserver is not suitable for this kind of thing.

Gunicorn is a relative upgrade to Server.

Help me scale worker, help me reboot with Python framework flask/django/webpy configuration is almost the same. There are also signaling mechanisms. Multiple configurations can be supported. Others: In the management of workers, the pre-fork model is used, that is, a master process manages multiple worker processes, and all requests and responses are handled by workers. The Master process is a simple loop that listens for worker signals from different processes and responds to them. For example, receiving TTIN increases the number of workers, while TTOU reduces the number of running workers. If the worker hangs and issues a CHLD, the failed worker is restarted and the synchronized worker processes one request at a time.

Why nginx

01. Load balancing. Frameworks such as Tornado only support a single core, so multi-process deployments require reverse load balancing. Gunicorn is a multi-process entity that does not require static file support. Nginx can be configured to handle static file requests without going through a Python server. The Python server can also return a special http01 header that will rewrite the static files. I mean after you configure it, did you configure it? 03, anti concurrent pressure. It doesn’t improve QPS, but an extra front-end layer can absorb some instantaneous concurrent requests, allowing Nginx to hold the connection while the back-end digests it. But honestly, the service experience in this case is pretty bad. But it’s better than a service failure. 04. Other features like rewrite. It has to be configured. Is it equipped?

The communication between the server and the client is briefly divided into three parts: request, request handling, and response. That is, the client issues the request to the server, the server responds and handles the request, and the result of the request is returned to the client. For Web sites or services, since request and response delay are uncontrollable, we need to consider the case of handling client requests with high latency. These requests take over the process on the server side. Asynchronous server software like Nginx is good at handling large numbers of requests with very little memory and CPU overhead. Since they are good at handling a large number of client requests at the same time, slow client requests do not affect them as much, so blocking Nginx in front of the pre-forking service for processing requests is a good choice. Nginx can respond to client requests asynchronously and with high concurrency (slow client requests have little effect on Nginx). Once Nginx receives a request, it will immediately transfer it to Gunicorn service for processing, and then Nginx will send the result back to the client in the form of response. In this way, the whole server and client communication, from the original only through the Gunicorn synchronous communication, into based on NGINX and Gunicorn asynchronous communication, communication efficiency and concurrent ability has been greatly improved. For a website, in addition to the situation described above, we should also consider the hosting of various static files. Static files not only include CSS, JavaScript and other front-end files, but also include pictures, videos and all kinds of documents, so static files may be relatively large, or will be called more frequently. The hosting function of static files is to ensure that all kinds of static files can be normally loaded, previewed or downloaded. This is the slow client behavior that takes a long time to respond. Hosting static files with Gunicorn also severely affects Gunicorn’s responsiveness-which is what Nginx is good at, so leave the hosting of static files to Nginx.

comprehensive

Nginx buffers requests and responses. If Gunicorn were to serve the service directly, the browser would initiate a request. Given the unknown browser and network conditions, the HTTP request process would be slow, and Gunicorn would have to wait for the request to complete before actually processing the request, and then wait for the client to fully accept the request before moving on to the next one. Nginx caches requests made by clients until they receive a complete request and forward it to Gunicorn. After Gunicorn finishes processing the request, Nginx gets the response and sends it to the client. This is a process that Nginx is good at, but Gunicorn is not good at handling. Therefore, putting Gunicorn behind Nginx can effectively improve the processing power of Gunicorn.

reference

Django, Nginx + UWSGI, or Nginx + Gunicorn are the two options for deploying Django online. : https://www.jianshu.com/p/be2… What is the role of Nginx and Gunicorn on the server? : https://www.zhihu.com/questio… Nginx + uwsgi and nginx + gunicorn difference, how to deploy: https://www.jianshu.com/p/be2…