How does nginx+php perform requests? Introduction to working principle

青灯夜游
Release: 2023-04-05 12:04:01
forward
2412 people have browsed it

nginx How does php perform requests? The following article will introduce to you the working principle of nginx php execution request. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

php working principle

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

cgi protocol

cgi protocol is used to determine the webserver (such as nginx), which is the content distribution server delivery What data is coming and what format of data

php-cgi process interpreter

php-cgi is the cgi protocol process interpreter of php , each time you start, you need to go through the process of loading the php.ini file -> initializing the execution environment -> processing the request -> returning content to the webserver ->php-cgi process exit

fastcgi protocol

The fastcgi protocol is a supplement to the efficiency improvement of the cgi protocol. It is mainly optimized for the need to start a cgi interpreter process every time a request comes. It is no longer needed. The cgi interpreter process needs 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, and each worker process There is a cgi process interpreter used to execute PHP code

php startup and working principle

When starting phpfpm, the master process will be started. Load the php.ini file, initialize the execution environment, 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 the php.ini configuration is modified After restarting, a new worker process will be started to load the new configuration, and the existing process will be destroyed after the work is completed, thus achieving a smooth restart

nginx working principle

If you want to understand the principle of cooperation between nginx and php, you also need to first understand 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; #设置脚本文件请求的路径 } }
Copy after login

The overall meaning of the above server configuration is: every time nginx Monitor the URL request on port 80 and 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-> ;Request to the corresponding IP server and port->nginx listens 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 The master process listens to the nginx request->the master process assigns 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 entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How does nginx+php perform requests? Introduction to working principle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!