First, the proxy and a reverse proxy
Real life examples
1, forward proxy: visit google.com
As shown above, because google is a wall, we need vpn over the wall to access google.com.
vpn for "we", it can be perceived (we connect vpn)
vpn for "google servers", is imperceptible (google only know http requests come).
For people who can perceive, but the server imperceptible server, we call him forward proxy server.2, reverse proxy: through the reverse proxy load balancing
As shown above, when we visit the baidu.com, baidu have a proxy server, by the proxy server, you can do load balancing, routing to a different server.
This proxy server, for "we" is not aware of (we can only perceive to access Baidu's server is, do not know the middle there is a proxy server to do load balancing).
This proxy server, for "server1 server2 server3" is perceivable (proxy server load balancing routed to a different server)
for people who can not perceive, but the server is perceptible, we call him a reverse proxy server
to sum up
To put it plainly: "forward", "reverse" is relative to people's perception of it.
One can feel the agency is to forward proxy, people do not feel the agent is a reverse proxy.
Second, Nginx acquaintance with Php-fpm
What is Nginx
Nginx ( "engine x") is a high performance HTTP and reverse proxy server is a IMAP / POP3 / SMTP server.
What is Php-fpm
1, cgi, fast-cgi protocol
cgi history
Early webserver only deal with html and other static files, but with the development of technology, there have been other dynamic languages like php.
webserver could not handle, and how to do it? Then to the php interpreter to deal with it!
Php interpreter to handle well, but, php interpreter how to communicate with a webserver it?
In order to solve different language interpreter (e.g., PHP, Python interpreter) communicate with a webserver, so there cgi protocol. As long as you go to write cgi programs in accordance with the agreement, we will be able to communicate with webwerver of language interpreter. The php-cgi program.
Improved fast-cgi
With cgi agreement to resolve the problem php interpreter to communicate with the webserver, webserver can finally handle dynamic language.
However, webserver each receive a request, a cgi process will go fork, and then kill off the end of the request process. There are 10,000 such requests, you need to fork, kill php-cgi process 10,000 times.
There did not find it a waste of resources?
As a result, there has been an improved version of cgi, fast-cgi. After fast-cgi each finished processing the request will not kill off the process, but to keep this process, so that this process can handle multiple requests. So that each would not have to re-fork a process, and greatly improving the efficiency.
2. What is php-fpm
php-fpm-Fastcgi Process Manager that is PHP.
php-fpm is to achieve FastCGI, and provides a process management functions.
Process contains master process and worker processes two kinds of processes.
Only one master process, responsible for monitoring port, receives requests from Web Server, and the worker process is generally more (specific number according to the actual needs), are embedded within each process a PHP interpreter, PHP code is actually executed The place.Three, Nginx how to combine with Php-fpm
Above we said, Nginx does not only processing http requests, but also do the reverse proxy.
Nginx reverse proxy function by dynamically steering the rear end request Php-fpm.
Let's configure a new Nginx + Php-fpm
1, the configuration file nginx.conf
Enter nginx directory, edit the file nginx.conf.
As shown in nginx.conf last line, add the include file
2, the corresponding server was added
Above include entry path, adding a server.
image_1b08f7sqm1ub71es9jrr1v3h1emp2q.png-119kB
Below we explain the meaning of configuration items:
server {
listen 80; #监听80端口,接收http请求
server_name www.example.com; #就是网站地址
root /usr/local/etc/nginx/www/huxintong_admin; # 准备存放代码工程的路径
#路由到网站根目录www.example.com时候的处理
location / {
index index.php; #跳转到www.example.com/index.php
autoindex on;
}
#当请求网站下php文件的时候,反向代理到php-fpm
location ~ \.php$ {
include /usr/local/etc/nginx/fastcgi.conf; #加载nginx的fastcgi模块
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000; #nginx fastcgi进程监听的IP地址和端口
}
}复制代码
In short: when we visit www.example.com, the treatment process is like this:
www.example.com
|
|
Nginx
|
|
路由到www.example.com/index.php
|
|
加载nginx的fast-cgi模块
|
|
fast-cgi监听127.0.0.1:9000地址
|
|
www.example.com/index.php请求到达127.0.0.1:9000 | | 等待处理...复制代码
Here we enable the php php-fpm to process the request
Open the php-fpm.conf file, we see the following configuration:
image_1b08gcs3g1msg9mf1ie7rks1b7o37.png-96kB
Namely: php-fpm module listens 127.0.0.1:9000 port, waiting for the arrival of the request to deal with.
IV Summary
Combined with nginx php-fpm, the complete process is such.
www.example.com
|
|
Nginx
|
|
路由到www.example.com/index.php
|
|
加载nginx的fast-cgi模块
|
|
fast-cgi监听127.0.0.1:9000地址
|
|
www.example.com/index.php请求到达127.0.0.1:9000 | | php-fpm 监听127.0.0.1:9000 | | php-fpm 接收到请求,启用worker进程处理请求 | | php-fpm 处理完请求,返回给nginx | | nginx将结果通过http返回给浏览器复制代码
Fifth, to show the effect of
1, starting with nginx module php-fpm
Successful start, we see php-fpm process
Above, there is a master process, three worker process.
2, create the file in the web directory
We edit the file as shown below:
3, visit the Web site