Fundamental:
Process browser to access the web
Requests for static pages
Browser(浏览器)
Request http://xxx.com/aa.html
-> Web Server (Nginx / Apache ) Distribution -> find the aa.html
file back to Browser
.
Requests a dynamic script
Browser
Request http://xxx.com/bb.php
-> Web Server (Nginx / Apache ) Distribution PHP解析器
-> ( PHP-CGI
program) -> return the results to the Web Server -> return data to the Browser.
Principle: According to the server 配置文件
, know that this is a PHP
script file, you need to go PHP解析器
to deal with.
PHP解析器
It parses the php.ini
file to initialize the execution environment, and then processes the request, and then return the results to the standard data format, and finally quit the process.
CGI programs to the evolutionary history of FPM
CGI(Common Gateway Interface)
CGI
Language to interact with the server back-end protocol, with this protocol, developers can use any language processing server forwards the request over the dynamically generated content, to ensure that the data is passed over 标准格式
the (specified 以什么样的格式传哪些数据(URL、查询字符串、POST数据、HTTP header等等
)) to facilitate the developer .
PHP-CGI(PHP CGI)
PHP语言
Corresponding to the interaction with the server CGI程序
is PHP-CGI
.
CGI程序
Itself only 解析请求
, and 返回结果
will not 进程管理
, so there is a fatal flaw, that is, each dealing with a request requires fork
a whole new process, along with Web
the rise of high concurrency increasingly become the norm, such inefficient way obviously can not meet demand (each time web请求
there will be 启动和退出进程
, that is the most criticized fork-and-execute
model, in such a large-scale concurrent, be dead).
In this way, FastCGI
it was born, CGI程序
and soon withdraw from the historical stage.
FastCGI(Fast CGI)
FastCGI
, By definition it is faster CGI程序
, to improve CGI程序
performance, which allows 在一个进程内处理多个请求
, rather than a request is processed directly to the end of the process has been greatly improved performance.
- Improve performance? Then the
CGI程序
performance problems lie?
PHP解析器
Parses the php.ini
file, initializing the execution environment, it is here.
Standard CGI程序
these steps for each request (not busy Leia! Start the process very tired to say!), So handle each request time will be longer. This is obviously unreasonable thing!
- So
FastCGI
it is how to do it?
First of all, FastCGI
we will first start a master进程
parsing configuration files, initialize the execution environment, and then start more worker进程
. When a request came, master
passed to a worker
, then the next request can be accepted immediately.
This will avoid duplication of work, efficiency naturally high.
And when worker
the time is not enough, master
you can be started in advance according to the configuration of several worker
more.
Of course, idle worker
time too many, also stopped a number, thus improving performance, but also saves resources. This is the FastCGI
management of the process.
ps: there are some capable of 调度PHP-CGI进程
programs, such as the lighthttpd
isolated spawn-fcgi
. Well, PHP-FPM
is such a stuff, after a long period of development, has been gradually recognized by everyone (but you should know a few years ago to complain about PHP-FPM
poor stability), are increasingly popular.