Current location - Education and Training Encyclopedia - Resume - How to understand the relationship between Nginx, uWSGI and Flask
How to understand the relationship between Nginx, uWSGI and Flask
In a word, the client has to go through three levels from sending HTTP request to Flask processing request: web server layer, WSGI layer and web framework layer. Different levels have different functions. Let's briefly introduce the functions of each level.

Figure1:Three-layer relationship among 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 and processes the request, and then returns a response to the client. In this process, the role of the server is:

Receive a request

Handle a request

Return response

Web server is a special server whose main function is to receive HTTP requests and return responses. When it comes to web servers, everyone will be familiar with it. Common web servers are Nginx, Apache, IIS, etc. In the three-tier structure of 1 above, the web server is the first to receive the user's request and return the response result to the user.

Web framework layer

The main function of the Web framework is to facilitate us to develop web applications, and the dynamic data requested by HTTP is provided by the web framework layer. Common web frameworks include Flask, Django, etc. We take the Flask framework as an example to illustrate the role of the web framework:

computer programming language

Import flask from flask

app = Flask(__name__)

@app.route('/hello ')

def hello_world():

Return to "Hello, World!"

if __name__ == '__main__ ':

App.run (host ='0.0.0.0', port =8080)

1

2

three

four

five

six

seven

Import flask from flask

app = Flask(__name__)

@app.route('/hello ')

def hello_world():

Return to "Hello, World!"

if __name__ == '__main__ ':

App.run (host ='0.0.0.0', port =8080)

The above few simple lines of code create a web application object app. The application monitors all IPS of the 8080 machine.

Port, which accepts the user's connection request. As we know, the HTTP protocol uses URL to locate resources, and the above program will give the request of path /hello to hello_world.

Method, hello_world returns "hello world!" String. For users of the web framework, they don't care how to receive HTTP.

Request, regardless of how to route the request to a specific method and return the response result to the user. In most cases, users of Web frameworks only need to care about how to implement business logic.

WSGI layer

WSGI is not a server, nor an API for interacting with programs, nor is it real code. WSGI is just an interface, which only applies to Python language.

Web server gateway interface, which defines the interface specification between web server and web application. That is, as long as the web server and

Web application follows WSGI protocol, so web server and Web application can be combined at will.

The following code shows how a web server can be integrated with a web application.

computer programming language

Define the application (env, start_response):

start_response('200 OK ',[('Content-Type ',' text/html')])

return [b"Hello World"]

1

2

three

Define the 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, and the parameters env and start_response are implemented and passed in by the web server. Among them,

Env is a dictionary, which contains environment variables such as HTTPHOST, HOSTUSERAGENT and SERVERPROTOCO.

Start_response is a method that accepts two parameters, namely status and response_headers.

The main function of the application method is to set the header information such as the status code and Content-Type of the http response, and return the specific results of the response.

The above code is a complete WSGI application. When a web server supporting WSGI receives a request from a client, it will call this application.

Method. The WSGI layer doesn't need to care about how env and start_response are implemented, just like in application.

What are you doing in it? Just use these two variables directly.

It is worth pointing out that WSGI is a protocol, and several similar nouns need to be distinguished:

Uwsgi: It is also a protocol similar to wsgi, and uWSGI server uses uwsgi protocol.

UWSGI: a web server that implements uWSGI and WSGI protocols. Note that uWSGI is also a kind of web server in essence, which is in the web server layer of the above three-tier structure.

CGI: Universal gateway interface, not limited to Python language, which defines how a web server provides dynamic content to clients. For example, it specifies how the client passes parameters to the web server, how the web server passes parameters to the web application, how the web application sends its output to the client, and so on.

Web applications in the production environment do not use CGI, and CGI processes (similar to Python interpreters) are created for each request and discarded when they are used up, which is inefficient. WSGI appeared only to replace CGI.

Having said that, we have basically made clear the role of WSGI between Web server and web framework: WSGI is like a link, connecting web server and web framework.

Web framework. Back to the topic of this article, Nginx belongs to the web server and Flask belongs to the web framework. Therefore, WSGI and

The role of Nginx and Flask is self-evident.

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 handle this request.

WSGI: OK, Nginx. I will set the environment variable and then pass this request to Flask for processing.

Thank you, WSGI! Give me some time, and I will return the response to the request to you.

WSGI: OK, I'll wait for you.

Flask: OK, I'm done. The following is the response result of the request, and it is requested to pass the result to Nginx. WSGI: Well done!

Nginx, here is the response result, which has been sent back to you as requested.

Nginx: Cool, I received it, and I returned the response result to the client. Everyone has a good cooperation ~