Home>Article>Backend Development> Introduction to the principle of nginx and php executing requests

Introduction to the principle of nginx and php executing requests

不言
不言 forward
2019-02-25 09:53:21 2188browse

This article brings you an introduction to the principles of nginx and php execution requests. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

First of all, let’s understand the relationship between cgi, php-cgi, fastcgi and php-fpm that we often hear, and help us understand the working principle of php

cgi protocol

The cgi protocol is used to determine the webserver (such as nginx), that is, what data is passed by the content distribution server and what format the data is in.

php-cgi process interpreter

php-cgi is the cgi protocol process interpreter of php. Every time it is started, it needs to load the php.ini file -> initialize the execution environment -> process the request -> return content to the webserver -> ;php-cgi process exit process

fastcgi protocol

The fastcgi protocol is a supplement to the efficiency improvement of the cgi protocol, mainly for each request. It is necessary to start the optimization of a cgi interpreter process. It is no longer necessary for the cgi interpreter process to reload the php.ini file and initialize the execution environment every time it receives a webserver request.

php-fpm Process Manager

php-fpm is the implementation of the fastcgi protocol and is a process manager. It includes master and worker processes at startup. The master process listens on the port and receives requests from the webserver. There are generally multiple worker processes, each worker Each process has a cgi process interpreter, which is used to execute PHP code

php startup and working principle

When starting phpfpm, the master process will be started, the php.ini file will be loaded, and the execution environment will be initialized. And start multiple worker processes. Each time a request comes, the request will be passed to the worker process for processing

php smooth restart principle

Every time after modifying the php.ini configuration and restarting, a new worker process will be started to load the new Configuration, and the previously existing process will be destroyed after the work is completed, so a smooth restart is achieved

nginx working principle

If you want to understand the principle of cooperation between nginx and php, you need to understand it first The server part in the nginx configuration file

server { listen 80; #监听80端口,接收http请求 server_name www.example.com; #一般存放网址,表示配置的哪个项目 root /home/wwwroot/zensmall/public/; # 存放代码的根目录地址或代码启动入口 index index.php index.html; #网站默认首页 #当请求网站的url进行location的前缀匹配且最长匹配字符串是该配置项时,按顺序检查文件是否存在,并返回第一个找到的文件 location / { #try_files,按顺序检查文件是否存在,返回第一个找到的文件 #$uri代表不带请求参数的当前地址 #$query_string代表请求携带的参数 try_files $uri $uri/ /index.php?$query_string; #按顺序检查$uri文件,$uri地址是否存在,如果存在,返回第一个找到的文件;如果都不存在,发起访问/index.php?$query_string的内部请求,该请求会重新匹配到下面的location请求 } #当请求网站的php文件的时候,反向代理到php-fpm去处理 location ~ \.php$ { include fastcgi_params; #引入fastcgi的配置文件 fastcgi_pass 127.0.0.1:9000; #设置php fastcgi进程监听的IP地址和端口 fastcgi_index index.php; #设置首页文件 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #设置脚本文件请求的路径 } }

The overall meaning of the above server configuration is: every time nginx listens to a URL request on port 80, it will perform location matching on the URL. If the / rule is matched, the internal request will be redirected and the internal request of /index.php?$query_string will be initiated, and the corresponding location configuration rule will send the request to the master process of php-fpm listening on port 9000

Summary

The following summarizes the simplest user request process:

User accesses the domain name->Domain name performs DNS resolution->Requests to the corresponding IP server and port->nginx Listen to the request of the corresponding port->nginx performs location matching on the URL->Execute the rules under the matching location->nginx forwards the request to php->php-fpm’s master process monitors the nginx request->master process Assign the request to one of the idle worker processes ->The worker process executes the request->The worker process returns the execution result to nginx->nginx returns the result to the user

The above is the detailed content of Introduction to the principle of nginx and php executing requests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete