Home > Backend Development > PHP Tutorial > A detailed introduction to the operating mechanism and principles between PHP and nginx

A detailed introduction to the operating mechanism and principles between PHP and nginx

不言
Release: 2023-04-05 17:42:01
forward
3283 people have browsed it

This article brings you a detailed introduction to the operating mechanism and principles between PHP and nginx. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Popularize knowledge points related to Nginx and Php-fpm

What is Nginx

Nginx ("engine x") is a high-performance HTTP and response server A proxy server that is also an IMAP/POP3/SMTP server.

What is Php-fpm

1. cgi, fast-cgi protocol

History of cgi

Early webservers only processed static files such as html. But with the development of technology, dynamic languages ​​​​such as php have emerged.
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.

Improvements of fast-cgi

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.

2. What is php-fpm

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.

2. How to combine Nginx with Php-fpm

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

1. Configure the nginx.conf file

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

2. Add the corresponding server

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地址和端口
    }

}
Copy after login

In short: when we visit www.example.com, the processing flow is Like this:

 www.example.|
        ||
        |.example.com/index.|
        |-|
        |-cgi监听127.0.0.1:|
        |.example.com/index.php请求到达127.0.0.1:9000
        |
        |
   等待处理。。。
Copy after login

Let’s enable php-fpm of php to handle this request

Open the php-fpm.conf file, let’s see Go to 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.

3. Summary

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返回给浏览器
Copy after login

4. Effect display

1. Start nginx and php-fpm modules

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

This article has ended here. For more exciting content, you can pay attention to the PHP Video Tutorial column on the PHP Chinese website!

The above is the detailed content of A detailed introduction to the operating mechanism and principles between PHP and nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template