Home  >  Article  >  Backend Development  >  How nginx and php work

How nginx and php work

小云云
小云云Original
2018-03-01 13:58:203024browse

When nginx receives an http request, it finds the corresponding server through the configuration file. Then match all locations in the server and find the best match. The command in location will start different modules to complete the work, such as rewrite module and index module. Therefore, modules in nginx can be regarded as real workers. The nginx module is compiled into nginx in a static way. When starting nginx, the module is automatically loaded. Unlike apache, the module is compiled into an so file separately, and whether to load it is specified in the configuration file. Therefore, nginx is also faster than apache in terms of module loading.

How does nginx call php? First look at the configuration of php in nginx below

location ~ \.php$ {
            root           /webpath;
            fastcgi_pass   127.0.0.1:9000;
            …
            ...
        }

This location directive hands requests with php as the file suffix to 127.0.0.1:9000 for processing. I think you should have guessed when you saw this, this is a C/S architecture thing. The IP address and port here (127.0.0.1:9000) are the IP address and port that the fastcgi process listens to. fastcgi is a scalable, high-speed communication interface between http servers and dynamic scripting languages. Most popular http servers support fastcgi, including apache, nginx, lighttpd, etc. At the same time, fastcgi is also supported by many scripting languages, including php.

So where do the configuration IP and ports of this fastcgi come from? You can see the following in php-fpm.conf:

listen = 127.0.0.1:9000  #这个表示php的fastcgi进程监听的ip地址以及端口

pm.start_servers = 2
php-fpm作为fastcgi的进程管理器,可以有效控制内存和进程,并且平滑重载php配置。php5.3以后,php-fpm被集成到php的core中,默认安装,无须配置。

The fastcgi process manager php-fpm initializes itself, starts the main process php-fpm and starts the start_servers fastcgi sub-processes. The main process php-fpm mainly manages the fastcgi sub-process, listens to port 9000, and the fastcgi sub-process waits for requests. When the client request reaches nginx, nginx passes all files with php as the suffix to 127.0.0.1:9000 for processing through the location directive. php-fpm selects and connects to a fastcgi child process and sends environment variables and standard input to the fastcgi child process. After the fastcgi subprocess completes processing, it returns standard output and error information. When the fastcgi child process closes the connection, the request is processed and is waiting for the next processing.

Ordinary cgi mode is a process, but there is a problem. Every time a request comes, a process must be forked, resulting in slow efficiency.

That’s why fast cgi fpm is fast cgi manager
fpm's mode is also a process, but it will prefork, that is, when the service starts, it first forks some processes to wait for processing requests.

Related recommendations:

Start, restart, and stop scripts for Nginx and PHP-FPM

Set nginx and PHP upload file size limits

Tutorial on installing nginx and php_PHP under mac

The above is the detailed content of How nginx and php work. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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