-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
最近在看nginx,加上自己的理解,整理下关于整个nginx,php-fpm的调用流程;
1.1 Nginx不只有处理http请求的功能,还能做反向代理;
1.2 Nginx通过反向代理功能将动态请求转向后端Php-fpm;
1.3 对于nginx来说,php只是一个运行在9000端口的程序而已;
- php访问动态web过程:
用户从浏览器发起对web的访问(http://xx.xxx.com), 用户和nginx服务器进行三次握手进行TCP连接;- nignx将http请求信息发送给nginx服务器;
- nginx根据用户访问url和后缀对请求进行判断;
(即当客户端请求到达Web Server Nginx时,Nginx通过location指令,将所有以php为后缀的文件都交给127.0.0.1:9000来处理,即Nginx通过location指令,将所有以php为后缀的文件都交给127.0.0.1:9000来处理) - nginx将请求交给fastcgi,通过fastcgi_pass将用户的请求发送给php-fpm
如果用户访问的是静态资源呢,nginx直接将用户请求的静态资源返回给用户; - fastcgi_pass将动态资源交给php-fpm后,php-fpm会将资源转给已等待的php脚本解析服务器的wrapper;
- wrapper收到php-fpm转过来的请求后,wrapper会生成一个新的线程调用php动态程序解析服务器,如果用户请求的是需要读取例如MySQL数据库等,将会触发读库操作;如果用户请求的是如图片/附件等,PHP会触发一次查询后端存储服务器如通过NFS进行存储的存储集群;
- php会将查询到的结果返回给nginx;
- nginx构造一个响应报文将结果返回给用户;
server
{
listen 80; #监听80端口,接收http请求
server_name xx.xxx.com;
index index.html index.htm index.php;
root /var/ProjectAAA/www/;
location ~ ^.*/(include|includes|tem.comtes)/.* {
deny all;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?$1.comst;
}
#当请求网站下php文件的时候,反向代理到php-fpm
location ~ .*\.php?$
{
include fcgi.conf;#加载nginx的fastcgi模块
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;#nginx fastcgi进程监听的IP地址和端口
fastcgi_index index.php;
}
location ~ .*\.(sh|pl|rar|zip|doc|asp|aspx|file|old|bak|tpl|dat) {
deny all;
}
error_page 404 /404.html;
error_page 403 /404.html;
error_page 502 503 504 /404.html;
access_log /var/log/httpd/xx.xxxx.xxx_access_log main;
}
注:
- fastcgi_pass指令,这个指令用于指定fpm进程监听的地址,
Nginx会把所有的php请求翻译成 fastcgi 请求之后再发送到这个地址; - include fcgi.conf #表示nginx会调用fastcgi接口
fastcgi_pass 127.0.0.1:9000;
#表示nginx通过fastcgi_pass将用户请求的资源发给127.0.0.1:9000进行解析,这里的nginx和php脚本解析服务器是在同一台机器上,所以127.0.0.1:9000表示的就是本地的php脚本解析服务器。nginx会调用php相关脚本解析程序对用户访问的资源进行解析;
nginx与php-fpm完整的流程:
xx.xxx.com
|
|
Nginx
|
|
路由到xx.xxx.com/index.php
|
|
加载nginx的fast-cgi模块
|
|
fast-cgi监听127.0.0.1:9000地址
|
|
xx.xxxx.com/index.php请求到达127.0.0.1:9000
|
|
php-fpm 监听127.0.0.1:9000
|
|
php-fpm 接收到请求,启用worker进程处理请求
|
|
php-fpm 会将资源转给php脚本解析服务器的wrapper
|
|
wrapper将启动一个新的进程,新的进程来调用php-cgi
|
|
php-fpm 处理完请求,返回给nginx
|
|
nginx将结果通过http返回给浏览器
Metadata
Metadata
Assignees
Labels
No labels