Gateway Protocol: CGI and WSGI

Usually the server program is divided into web servers and application servers. A web server is used to process HTML files, so that customers can access through a browser, there are mainstream web server Apache, IIS, Nginx, lighthttpd and so on. Application server processing business logic, such as the use of Python Django, flask written procedures. Usually a request from the client browser is intercepted by a web server, if it is static request, such as Nginx will make their own treatment, if it is dynamic request, will be thrown to the back-end application server to handle. So how it has become a major problem in the communication between the web server and application server, which leads to the treatment of the following three interfaces: CGI, FastCGI, WSGI.

CGI

Common Gateway Interface (Comman Gateway Interface) describes a standard for transmitting data between the client and server programs, allowing a client to request data from the web browser application executing on a web server. CGI is independent of any language, CGI programs can be completely independent of any script language or programming language, as long as the language can run on this system. Unix shell, Python, Ruby, PHP, Perl, C / C ++ and VB can be used to write CGI programs. CGI was originally developed in 1993 by the National Center for Supercomputing Applications computer to NCSA HTTPd web server. The web server uses Unix shell environment variable to hold the parameters passed out from a web server, and then generate a separate process to run the CGI. CGI processing flow as shown below:

  • web server receives the client (browser) requests http request, start the CGI program, and environment variables, data input transfer standards;
  • CGI parser process starts, loading configuration (e.g., service configuration), other servers (such as a database server), a logic processing;
  • CGI process the processing result output by the standard, standard error, is passed to the web server;
  • CGI web server receives the results returned to construct http response back to the client, and kill the process of CGI, a web server and CGI environment variables, the standard input, standard output and standard error to transfer data;

CGI between the external programs and web server interaction is possible. CGI programs to run in a separate process, and for each web request to establish a process, this method is very easy to implement, but the efficiency is poor, it is difficult to expand. The face of a large number of requests, and the demise of a large number of the establishment process of the operating system performance declined significantly. In addition, because the address space can not be shared, industry limits the resource reuse.

FastCGI

Fast Common Gateway Interface (Fast Comman Gateway Interface) is to improve the Common Gateway Interface (CGI), and describes a standard for transmitting data between the client and server programs. FastCGI is committed to reducing the interaction between the web server and CGI program overhead, allowing the server to handle more simultaneous web requests. And to buy a new request to create a different process, FastCGI use ongoing process to deal with a series of requests. These processes by the FastCGI Process Manager, instead of the web server.

When a request comes in, web server environment variables and request this page via Unix domain socket (located in the same physical server) or a IP socket (FastCGI deployed in different physical server) passed to the FastCGI process.

  • Loading initialization FastCGI execution environment when the web server is started, such as IIS ISAPI, Apache mod_fastcgi, nginx_ngx_http_fastcgi_module, lighthttpd mode_fastcgi;
  • FastCGI process manager initializes itself, to start multiple CGI process the interpreter and waits for a connection from a web server. When you start FastCGI process, you can configure ip socket or Unix domain socket between the two methods;
  • When a client request arrives at the web server, web server requests sent to the embodiment using FastCGI socket main process, FastCGI selected and connected to the main process a CGI interpreter. web server sends to the child FastCGI CGI environment variables and the standard input;
  • FastCGI child process after the completion of treatment standard output and error messages from the same connection returns web server socket. When the child FastCGI process closes the connection request will be processed;
  • FastCGI process and the child process then waits for the next connection from the web server;

Because FastCGI process does not need to constantly generate new process, it can greatly reduce the pressure on the server and generate higher application efficiency. Its speed than CGI technology to improve the efficiency of at least more than 5 times. It also supports a distributed deployment, namely FastCGI program can run on a host other than the web server.
CGI is called the short lifetime of the application, FastCGI application is called long-lived. FastCGI like a permanent (long-live) type of CGI, it can always perform with, will not always have to spend the time to fork (this is the most criticized CGI fork-and-execute mode).

WSGI

web server gateway interface (Python Web Server Gateway Interface) is a Python language for custom between the web server and web application framework or simple common interface. Since WSGI been developed, and many other languages, there have been similar interface. WSGI as a low-level between the web server and web application or application framework interface to enhance common portable web application development. WSGI CGI is based on existing standards and design.
WSGI divided into two parts: one for the "server" or a "gateway", another for the "application" or "application framework." WSGI when processing a request, the server will provide environmental information and a callback function (callback function) for the application. When the application has finished processing the request, through the callback function, the results back to the server. The so-called WSGI middleware while achieving a two-party API, so you can play a mediating role between the service and the WSGI WSGI applications: from the perspective of WSGI server, the application middleware plays, and from an application point of view, the middle piece plays servers. "Middleware" components can perform the following functions:

  • Rewriting environment variable, according to the target URL, it will request message is routed to a different application objects;
  • Allowed to run simultaneously in a plurality of application process or application framework;
  • Load balancing and remote processing, by forwarding the request and response messages over a network;
  • After processing the content, such as application XSLT stylesheet;

Previously, how to choose the right web application framework has become a problem for Python beginners, this is because, in general, choose a web application framework will limit the choice available to the web server, and vice versa. Then Python applications usually CGI, FastCGI, mod_python of a design, or even to a particular API interface custom web server designed. WSGI no official implementation, because more like a WSGI protocol. Just follow these protocols, WSGI application (application) can be run on any server (server), and vice versa. WSGI is a Python package of CGI, FastCGI is relative to the PHP CGI package.
The web component WSGI divided into three categories: web server, web middleware, web applications, WSGI basic processing mode:

. 1, wsgi Server / Gateway
wsgi Server can be understood as a specification of compliance WSGI web server, receiving a REQUEST request, encapsulating a series of environmental variables, in accordance with the specification calls registered wsgi wsgi App, and finally the response back to the client. The text is difficult to understand clearly wsgi server in the end what it is, and do something, most intuitive way is to see wsgi server implementation code. Python comes with wsgiref for example, wsgiref is a simple wsgi server implemented in accordance with wsgi specification.

  • Server creates a socket, listening port, waiting for client connections;
  • When there is a request to the server to resolve the client information into the environment variable environ and call handler bound to process the request;
  • http handler parses the request, the request information such method, path, etc. into the environ;
  • wsgi handler then some servers information is also put environ, the last server information, client information, this request all the information is saved to the environment variable environ in;
  • wsgi handler calls registered wsgi app, and passed to the callback function and environ wsgi app;
  • wsgi app will response header / status / body back to wsgi handler;
  • The final handler or through the socket response information back to the client;

2, the Application WSGI
WSGI the Application is an ordinary callback object when a request comes in, wsgi server will call this wsgi app. This object receives two parameters, typically environ, start_response. environ is introduced above, it can be understood as an environment variable, with a request for all information related to the environment are stored in the variables, including server information, client information, request information. start_response is a callback function, wsgi application by start_response, the response header / status back to wsgi server. In addition to this wsgi app will return an iterator object, the iterator object is the response body.

3, wsgi middleware
some features may be between servers and applications, for example, the server got the URL requested by the client, different URL needs referred to different processing functions, this feature called URL routing, this function can be put implemented both in the middle of this intermediate layer is the middleware. middleware servers and applications are transparent, that is to say, it is the server program that the application, and the application that it is the server. This tells us, middleware need to disguise himself as a server, receives an application, call it, at the same time middleware also need to pretend to be an application to the server program. In fact, whether it is the server program, middleware or applications, all in the service side, customer service, reason why they are abstracted into different layers, is to control the complexity, so that each time less complex, carry out their duties.

uwsgi

uwsgi designed to network application development and deployment of distributed cluster of a complete solution. uwsgi mainly for web standards and services, has been successfully applied in many different languages. Because uwsgi scalable architecture, it can be unlimited extension to support more platforms and languages. Currently you can use C, C ++ and Object-C to write plug-ins. Wsgi project name in order to thank the same name Python web standards, because wsgi for the project developed the first plug-in. uwsgi is a web server that implements wsgi agreement, uwsgi protocol, http and other protocols. uwsgi protocols do not neither wsgi FastCGI protocol, but a uwsgi own protocol, a protocol is uWSGI uwsgi free server protocol, which is used to define the type of information transmission, before each uwsgi packet 4 to transmit information type byte description, it is compared with WSGI are two things, this protocol is said to be about 10 times faster fcgi protocol. uWSGI main features are as follows:

  • Ultra-fast performance;
  • (Measured as about half of mod_wsgi Apache2 of) low memory footprint;
  • Multi-app management;
  • Detailed logging (app performance and can be used to analyze bottlenecks);
  • Highly customizable (memory size limit, a certain number of service after the restart, etc.);

Guess you like

Origin www.cnblogs.com/love9527/p/11440607.html