Detailed explanation of the working principle of nginx+php execution request

Detailed explanation of the working principle of nginx+php execution request

How PHP works

First, let's understand the relationship between the commonly heard cgi, php-cgi, fastcgi, and php-fpm to help understand the working principle of php

cgi protocol

The cgi protocol is used to determine what data and what format the webserver (such as nginx) sends to the content distribution server.

php-cgi process interpreter

php-cgi is the CGI protocol process interpreter of php. Each time it is started, it needs to go through the process of loading the php.ini file -> initializing the execution environment -> processing the request -> returning the content to the webserver -> exiting the php-cgi process

FastCGI Protocol

The fastcgi protocol is a supplement to the efficiency improvement of the cgi protocol. It is mainly aimed at optimizing the need to start a cgi interpreter process every time a request comes in. The cgi interpreter process no longer 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 an implementation of the FastCGI protocol. It is a process manager. When it starts, it includes two parts: the master process and the worker process. The master process listens to the port and receives requests from the webserver. There are usually multiple worker processes. Each worker process has a CGI process interpreter to execute PHP code.

PHP startup and working principle

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

PHP smooth restart principle

Each time the php.ini configuration is modified and restarted, 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.

How nginx works

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

server {
  listen 80; #Listen to port 80 and receive http requests server_name www.example.com; #Generally store URLs, indicating which project is configured root /home/wwwroot/zensmall/public/; #The root directory address of the code or the code startup entry index index.php index.html; #Default homepage of the website #When the URL of the requested website performs a prefix match on location and the longest matching string is this configuration item, check in order whether the files exist and return the first file found location / {
     #try_files, check in order whether the files exist, and return the first file found#$uri represents the current address without request parameters#$query_string represents the parameters carried by the requesttry_files $uri $uri/ /index.php?$query_string; #Check the $uri file in order to see if the $uri address exists. If so, return the first file found; if neither exists, initiate an internal request to access /index.php?$query_string, which will be re-matched to the following location request}
  
   #When requesting the php file of the website, the reverse proxy is sent to php-fpm to process location ~ \.php$ {
     include fastcgi_params; #Introduce fastcgi configuration file fastcgi_pass 127.0.0.1:9000; #Set the IP address and port that the php fastcgi process listens to fastcgi_index index.php; #Set the home page file fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #Set the path of the script file request}
}

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

Summarize

The following summarizes the simplest user request process:

User accesses domain name -> DNS resolution of domain name -> request to corresponding IP server and port -> nginx listens to request of corresponding port -> nginx matches location of URL -> executes rules under matching location -> nginx forwards request to php -> php-fpm master process listens to nginx request -> master process assigns request to one of idle worker processes -> worker process executes request -> worker process returns execution result to nginx -> nginx returns result to user

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the principle and implementation process of Nginx configuration https
  • Nginx URL rewriting mechanism principle and usage examples
  • Difference and principle analysis of Nginx forward and reverse proxy
  • Explore the truth behind the reload process in Nginx
  • Nginx server load balancing and ssl principle, generate ssl key pair, Nginx configuration ssl operation example
  • Detailed explanation of how Nginx works

<<:  5 Tips for Protecting Your MySQL Data Warehouse

>>:  How to use Antd's Form component in React to implement form functions

Recommend

How to backup and restore the mysql database if it is too large

Command: mysqlhotcopy This command will lock the ...

CSS animation combined with SVG to create energy flow effect

The final effect is as follows: The animation is ...

How to generate a unique server-id in MySQL

Preface We all know that MySQL uses server-id to ...

How to use Font Awesome 5 in Vue development projects

Table of contents Install Dependencies Configurat...

MySQL 5.6.36 Windows x64 version installation tutorial detailed

1. Target environment Windows 7 64-bit 2. Materia...

Example of how to quickly build a Redis cluster with Docker

What is Redis Cluster Redis cluster is a distribu...

Detailed explanation of common for loop in JavaScript statements

There are many loop statements in JavaScript, inc...

Details of various font formats in HTML web pages

This section starts with the details of text modi...

Docker View the Mount Directory Operation of the Container

Only display Docker container mount directory inf...

How to decrypt Linux version information

Displaying and interpreting information about you...

Summary of some thoughts on binlog optimization in MYSQL

question Question 1: How to solve the performance...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...

How to create a new user in CentOS and enable key login

Table of contents Create a new user Authorize new...