Home > Article > Backend Development > The operating mechanism and principles between PHP and nginx
Nginx ("engine x") is a high-performance HTTP and reverse proxy server, also an IMAP/POP3/SMTP server.
Early webservers only processed html, etc. Static files, but with the development of technology, dynamic languages such as php have appeared.
The webserver cannot handle it, what should I do? Then let the php interpreter handle it!
It’s good to leave it to the PHP interpreter, but how does the PHP interpreter communicate with the webserver?
In order to solve the communication problem between different language interpreters (such as php, python interpreters) and webserver, the cgi protocol appeared. As long as you write a program according to the cgi protocol, you can achieve communication between the language interpreter and webwerver. Such as php-cgi program.
With the cgi protocol, the problem of communication between the php interpreter and the webserver is solved, and the webserver can finally handle dynamic languages.
However, every time the webserver receives a request, it will fork a cgi process, and then kill the process after the request is completed. If there are 10,000 requests, the php-cgi process needs to be forked and killed 10,000 times.
Have you ever found it to be a waste of resources?
So, an improved version of cgi, fast-cgi, appeared. After fast-cgi processes a request each time, it will not kill the process, but retain the process so that the process can handle multiple requests at one time. In this way, there is no need to re-fork a process every time, which greatly improves efficiency.
php-fpm is php-Fastcgi Process Manager.
php-fpm is the implementation of FastCGI and provides process management functions .
The process includes two types of processes: master process and worker process.
There is only one master process, which is responsible for listening to the port and receiving requests from the Web Server. There are generally multiple worker processes (the specific number is configured according to actual needs). Each process has a PHP interpreter embedded inside it, which is PHP Where the code actually executes.
We know that Nginx not only has the function of processing http requests, but also can be used as a reverse proxy.
So Nginx forwards dynamic requests to the backend Php-fpm through the reverse proxy function.
Let’s configure a new Nginx Php-fpm
Enter the nginx directory and edit the nginx.conf file.
As shown in the figure, in the last line of nginx.conf, add the include file
Enter the include path above and add A server.
Let’s explain the meaning of the 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. For example.com, the processing flow is as follows:
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 | | 等待处理。。。
Next we enable php-fpm of php to handle this request
Open the php-fpm.conf file, we see the following configuration:
That is: the php-fpm module listens to the 127.0.0.1:9000 port and waits for the request to be processed. .
The combination of nginx and php-fpm, the complete process is like this.
nginx与php-fpm的结合,完整的流程是这样的。 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返回给浏览器
Start successfully, let’s check the php-fpm process
##As shown above, there is one master process and three worker processes. 2. Create a file in the website directory We edit the file as shown below: 3. Visit the website
【Related tutorials: PHP video tutorial】
The above is the detailed content of The operating mechanism and principles between PHP and nginx. For more information, please follow other related articles on the PHP Chinese website!