Brief description CGI And what are dynamic requests
1. CGI What is it?
CGI yes common gateway interface Abbreviation of , Everyone translates it as a universal Gateway Interface , But unfortunately , We can't see the meaning of fame .
We know ,web The content processed by the server is static , To process dynamic content , Need to rely on web application program , as php,jsp,python,perl etc. . however web
server How to pass dynamic requests to these applications ? It depends on cgi agreement . you 're right , It's an agreement , that is web
server and web Specification for application communication . let me put it another way , adopt cgi agreement , Combined with the built web application program , You can make web
server Yes " handle " Dynamic request ( Or say , When a user accesses a specific resource , Can trigger the execution of a web Application to implement specific functions ), You must know why you need to put double quotes when dealing with two words .
Simple version cgi Work as follows ：
for example , Search for a keyword in Google search "http", Corresponding URL by ：
When Google web
server After receiving the request , Analyze the url, From which I know to execute search program , And I know a series of things to pass on to search Parameters of and their corresponding value.web
server These program parameters and other environment variables will be cgi Agreement passed TCP Or socket to the started cgi program ( It could be cgi process , Or loaded modules cgi modular ). When cgi Process received web
server After , call search Procedure and execution , Parameters are also passed to search program .search After execution ,cgi process / Thread returns processing results to web
server,web server Back to browser .
There are many ways to do this cgi program , But yes http For the request method of , only get and post Two methods allow execution cgi script ( It's the top one search program ). actually post The internal nature of the method is get method , Just sending http On request ,get and post Method right url Parameters in are handled differently .
Can be written in any language CGI, But some languages are good at it , Some languages are very cumbersome , For example bash
shell development , So we need to use echo Wait for the print statement to put the execution result in a huge number of incomparable html Output to client in tag of . Often used for writing CGI The language of perl,php,python etc. ,java Can also write , but java Of servlet Fully achievable CGI Function of , And more optimized , Better for development .
2. Explanation of various terms
tell the truth , For someone who hasn't been exposed to a programming language , Rigid contact cgi There must be a lot of questions about the concept , What the hell is this , Things that deal with dynamic content are not like php The same application , Follow cgi How much is it ,fastcgi What is it ? I think , Obsessive-compulsive disorder patients of non academic background ( Including me ) It's bound to be tossed around by these concepts .
with php take as an example , I will explain the concept of a dynamic request briefly .
* cgi： It's a protocol . adopt cgi agreement ,web server Dynamic requests and related parameters can be sent to applications that specifically handle dynamic content .
* fastcgi： It's also a protocol , nothing but cgi Optimized version of .cgi Poor performance ,fastcgi On the basis of it, the improvement is made .
* php-cgi：fastcgi It's a protocol , and php-cgi This protocol is implemented . But this kind of implementation is bad . It's single process , A process processes a request , Process destroyed after processing .
* php-fmp： Yes, it is php-cgi Improved version of , It directly manages multiple php-cgi process / thread . in other words ,php-fpm yes php-cgi Process manager for
So it's kind of fastcgi Implementation of the protocol . To some extent ,php-fpm And php Relationship of , and tomcat Yes java The relationship is similar .
* cgi process / thread ： stay php upper , namely php-cgi process / thread . Dedicated to receiving web server Dynamic request for , Call and initialize zend virtual machine .
* cgi script ： Executed php Source code file .
* zend virtual machine ： Yes php Lexical analysis of documents , Grammatical analysis , Compile into opcode, And execute . Last closed zend virtual machine .
* cgi process / Threads and zend Virtual machine relationship ：cgi Process call and initialize zend Various environments of virtual machine .
with php-fpm take as an example ,web server The process from forwarding the dynamic request to the end is roughly as follows ：
And each php-cgi The role of the process includes ：( Some functions are classified incorrectly , Please ignore , It's enough to know the general function )
be careful , although php-fpm The full name of PHP FastCGI Process Manager, But strictly speaking ,php-fpm no fastcgi Process manager for , But php
fastcgi Namely php-cgi Process manager for .fastcgi It's just an agreement , Not a process . Like http The agreement is the same ,apache Its implementation is httpd,nginx The realization of it is called nginx.
Again ,cgi and fastcgi It's a protocol . Various support and WEB Interactive programming language pair cgi/fastcgi Each protocol has its own implementation ( of course , Can write in any language cgi script ), and php On php-cgi and php-fpm It is php Yes fastcgi Implementation of the protocol .
3. web server and CGI Interaction mode of
web server Yes cgi process / For threads , Its function is to initiate dynamic processing of requests , Pass some parameters and environment variables , Last received cgi Results returned by . In a more popular and less rigorous way ,web
server adopt cgi/fastcgi Protocol forwards dynamic request to execution cgi Script application . By httpd.conf The forwarding configuration in should be easy to understand (httpd and php-fpm Interaction of )：
ProxyRequests off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr
With the most typical apache httpd and php take as an example , about httpd In terms of ,web server and php-cgi Yes 3 Interaction modes .
* cgi pattern ：httpd Receive a dynamic request fork One cgi process ,cgi Process returns results to httpd Post process self destruction .
* Dynamic module mode
： take php-cgi Module of ( for example php5_module) Compile into httpd. stay httpd Module loaded on startup , The corresponding module will also be activated when loading ,php-cgi And it started .( notes ： Correct a small mistake , Many people think that dynamically compiled modules can be loaded and called at any time when they need to , They stop when they don't need to , Not really . Same as statically compiled modules , Dynamically loaded modules are added to the activation list when they are loaded , Whether you use it or not , It's all running on apache
httpd Inside of . Reference LoadModule Official Manual of the directive )
* php-fpm pattern
： use php-fpm Administration php-cgi, here httpd No more control php-cgi Start of process . You can php-fpm Independent operation in non web On the server , To achieve the so-called dynamic and static separation .
actually , With the help of modules mod_fastcgi It can also be realized fastcgi pattern . with cgi equally , The inborn defect of management mode determines that this is not a good method .
3.1 CGI pattern
use CGI In mode , When a dynamic request arrives ,httpd Temporarily start a cgi interpreter , And passed cgi Protocol forwarding what to run . When cgi After the script runs , Return results to httpd, then cgi Interpreter process self destructs . When multiple dynamic requests arrive , Multiple will be started successively cgi interpreter . therefore , This method is extremely inefficient .
In the comments php5_module Of LoadModule After related lines , use action Instruction specifies to use cgi Type of operation . But notice ,action The order is mod_action Provided , So the module must already be loaded .
for example ： appoint MIME Type is image/gif Requested use of images.cgi function . obviously ,images.cgi You have to write the script first .
Action image/gif /cgi-bin/images.cgi
You can also add handler To compound file types , Reuse a cgi Script to run this handler Any type in .
AddHandler my-file-type .xyz Action my-file-type "/cgi-bin/program.cgi"
about php In terms of , You can use the installation php Time bin Provided in the directory php-cgi Procedure as cgi program .
[[email protected] php]# ls /usr/local/php/bin/ pear peardev pecl phar phar.phar php
php-cgi php-config phpize# Copy to apache default cgi-bin Directory , Easy to manage [[email protected] php]# cp
/usr/local/php/bin/php-cgi /usr/local/apache/cgi-bin/ # stay httpd.conf Add the following lines to
Action application/x-httpd-php /usr/local/php/bin/cgi-bin/php-cgi
3.2 Module mode
Compiling php Time , take php5_module Module compiled to apache in , For example, when compiling php When ./configure Add in configuration "--with-apxs2=/usr/local/apache/bin/apxs".
In this interactive mode ,httpd Load and activate at startup php_module. in other words ,php-cgi Resident in httpd Inside the process . When a dynamic request arrives ,httpd No more generation cgi interpreter , Instead, it forwards the dynamic request directly to its internal php-cgi.
Configuration utility this interaction mode is very simple , Just use LoadModule load php_module, Add the corresponding MIME The processor can .
LoadModule php5_module modules/libphp5.so # stay mime Add corresponding type in module <IfModule
mime_module> AddType application/x-httpd-php .php AddType
3.3 php-fpm mode
As I said before ,php-fpm yes php-cgi Process manager for . This kind of interaction actually makes php-cgi Independent of httpd The way , Current basic use php-fpm How to manage php-cgi process . in other words , In this mode ,php-cgi and httpd It 's separated , Their separation means that the dynamic and static separation of the request becomes possible ：httpd and php-fpm Running on different servers . After dynamic and static separation , The pressure is also spread to the respective servers .
Let's php-fpm Run in this way , Need to be compiled in ./configure Add in configuration options "--enable-fpm" option . of course , It has to be started php-fpm service . for example ：
service php-fpm start
such php-cgi The process opens the port ( default 9000) wait for httpd Forward dynamic requests . Let's httpd Ability to forward requests to php-cgi upper , Need to be in httpd.conf Close forward agent in , And set fastcgi Protocol proxy parameters . for example , Forward to 192.168.100.54 On the host php-fpm.
# Load agent module LoadModule proxy_module modules/mod_proxy.so LoadModule
proxy_fcgi_module modules/mod_proxy_fcgi.so# add to MIME type AddType
application/x-httpd-php .phpAddType application/x-httpd-php-source .phps #
Configure the forwarding agent in the virtual host to be forwarded ProxyRequests off ProxyPassMatch ^/(.*\.php)$ fcgi:/