Fully understand the relationship between Nginx, WSGI, and Flask, nginxflask
Overview
I have doubts about the relationship between Nginx, WSGI (or uWSGI, uwsgi), and Flask (or Django. After reading some documents, we finally sorted out their relationships.
In summary, the client processes requests from sending an HTTP request to Flask, respectively through the web server layer, WSGI layer, and web framework layer. Different layers have different functions. The following describes the functions of each layer.
Layer-3 Relationship Between web servers, web frameworks, and WSGI
Figure 1: layer-3 Relationship Between web server, web framework and WSGI
Web server layer
For the traditional client-server architecture, the request processing process is that the client sends a request to the server, the server receives the request and processes the request, and then returns a response to the client. In this process, the role of the server is:
1. Receive requests
2. Process requests
3. Response
A Web Server is a special type of server. Its function is to receive HTTP requests and return responses. We are familiar with web servers. Common web servers include Nginx, Apache, and IIS. In the layer-3 structure of 1, the web server first receives user requests and returns the response results to the user.
Web framework layer
The Web framework is mainly used to facilitate the development of web applications. The dynamic data of HTTP requests is provided by the web framework layer. Common web frameworks include Flask and Django. We use the Flask framework as an example to demonstrate the functions of the web framework:
from flask import Flaskapp = Flask(__name__)@app.route('/hello')def hello_world(): return 'Hello World!'if __name__ == '__main__': app.run(host='0.0.0.0', port=8080)
After a few simple lines of code above, a web application object app is created. The app listens to port 8080 of all ip addresses of the machine and accepts user requests for connection. We know that the HTTP protocol uses a URL to locate the resource. The above program will submit the path/hello request to the hello_world method for processing, and hello_world will return 'Hello World! 'String. For web framework users, they do not care about how to receive HTTP requests, nor how to route requests to specific methods for processing and return response results to users. In most cases, Web framework users only need to care about how to implement the business logic.
WSGI Layer
WSGI is neither a Server nor an API for interacting with a program, nor a real code. WSGI is only an Interface and is only applicable to Python. It is called a Web Server Gateway Interface, defines interface specifications between web servers and web applications. That is to say, as long as both the web server and the web application comply with the WSGI protocol, the web server and the web application can be combined at will.
The following code demonstrates how a web server is combined with a web application.
def application(env, start_response):start_response('200 OK', [('Content-Type', 'text/html')])return [b"Hello World"]
The method application is called by the web server. The env and start_response parameters are implemented and passed in by the web server. The env is a dictionary that contains environment variables such as HTTP_HOST, HOST_USER_AGENT, and SERVER_PROTOCO. Start_response is a method that accepts two parameters: status and response_headers. The main function of the application method is to set the header information such as the http response status code and Content-Type, and return the specific response results.
The above code is a complete WSGI application. When a web server that supports WSGI receives a client request, it will call this application method. The WSGI layer does not need to care about how the env and start_response variables are implemented. Just like what is done in the application, you can directly use these two variables.
It is worth noting that WSGI is a protocol that must distinguish between several similar terms:
Uwsgi
The same as wsgi, The uWSGI server uses the uwsgi protocol.
UWSGI
Web servers that implement the uwsgi and WSGI protocols. Note that uWSGI is essentially a web server in the layer-3 structure described above.
CGI
The universal Gateway Interface, not limited to the Python language, defines how the web server provides dynamic content to the client. For example, it specifies how the client Passes parameters to the web server, how the web server passes parameters to the web application, and how the web application sends its output to the client.
Web applications in the production environment do not use CGI. The CGI process (similar to the Python interpreter) is created for each request and discarded when used up, resulting in low efficiency. WSGI emerged to replace CGI.
Speaking of this, we have basically clarified the role of WSGI between the web server and the web Framework: WSGI is like a link that connects the web server to the web framework. Back to the question in this article, Nginx is a web server, and Flask is a web framework. Therefore, the roles of WSGI, Nginx, and Flask are unknown.
Finally, we end this article with a dialogue between Nginx, WSGI, and Flask.
Nginx:Hey, WSGI, I just received a request. I need you to make some preparations and then Flask will process the request.
WSGI:OK, Nginx. I will set the environment variable and pass the request to Flask for processing.
Flask:Thanks WSGI! Give me some time and I will return the request response to you.
WSGI:Alright, then I will wait for you.
Flask:Okay, I have finished. Here is the request response result. The request passes the result to Nginx.
WSGI:Good job! Nginx: Here is the response result, which has been passed back to you as required.
Nginx:Cool, I received it. I returned the response to the client. Pleasant cooperation ~
The relationship between Nginx, WSGI, and Flask is all the content that I have shared with you in this article. I hope you can provide a reference and support for our customer base.