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

Call and execute host docker operations in docker container

First of all, this post is dedicated to Docker no...

The best solution for implementing digital plus and minus buttons with pure CSS

Preface: For the implementation of digital additi...

Implementation of Vue large file upload and breakpoint resumable upload

Table of contents 2 solutions for file upload Bas...

MySQL transaction details

Table of contents Introduction Four characteristi...

Summary of naming conventions for HTML and CSS

CSS naming rules header: header Content: content/c...

JS+CSS to realize dynamic clock

This article example shares the specific code of ...

Several ways to connect tables in MySQL

The connection method in MySQL table is actually ...

Sample code for implementing two-way authentication with Nginx+SSL

First create a directory cd /etc/nginx mkdir ssl ...

TypeScript decorator definition

Table of contents 1. Concept 1.1 Definition 1.2 D...

W3C Tutorial (9): W3C XPath Activities

XPath is a language for selecting parts of XML do...

MySQL 5.7.18 MSI Installation Graphics Tutorial

This article shares the MySQL 5.7.18 MSI installa...

Use pictures to realize personalized underline of hyperlinks

Don't be surprised if you see some kind of und...

Mysql implements null value first/last method example

Preface We already know that MySQL uses the SQL S...

Steps for docker container exit error code

Sometimes some docker containers exit after a per...