Home > Backend Development > PHP Tutorial > How does PHP receive the request?

How does PHP receive the request?

藏色散人
Release: 2023-04-07 07:22:02
forward
3096 people have browsed it

How does php receive the request?


This article mainly describes a few points

● How does nginx forward requests to PHPFPM?

● What exactly are CGI and FastCGI? Toy?

● What is PHPFPM? what's the effect?

Simple scene description

Visit a mall built with php nginx mysql on the browser and purchase an item.

Analysis (there are two resources accessed here)

● Static resources (some pictures, icons of the website, etc.)

● Dynamic resources ( The price of the purchased product, product introduction, etc.)

The browser initiates a request --> web_server (nginx) distribution processing --> PHP executes the code and returns the result (this is the approximate process)

How does nginx distribute requests?

When the user initiates a request (the browser requests port 80 by default), nginx listens to port 80, and configures the regular match through nginx whether it belongs to a static resource. If it is a static resource, the file will be returned and the request ends. . If it is a dynamic resource and the request php script is matched through regular expressions, it will distribute the request to PHPFPM for processing through the nginx module ngx_http_fastcgi_module, and then return the result after processing.

● CGI

CGI is a specification for web servers to run external programs. This means that you can communicate with your program through CGI, through the CGI standard format. Your program can interact with the browser.

(A simple understanding of CGI is a protocol, which stipulates how to transmit some things, how your program accepts processing, etc.)

● PHP-CGI

PHP-CGI is an implementation version of the CGI protocol php. PHP-CGI will fork a process for each request and exit after processing is completed. (This pattern is called fork-and-execute). This model is not in line with the current large-scale traffic, so it has withdrawn from the stage of history.

● FastCGI

FastCGI is an upgraded version of CGI. It will pre-start a master process to read the configuration file, and then fork multiple work processes to wait for connections. After listening to the request, a work process is allocated for specific processing. This greatly improves the performance of the program. (FastCGi will manage the process and will not destroy it easily after the processing is completed. CGI will create a process for each request and destroy the process.)

● PHPFPM

As the best language in the world, Of course you have to keep up with the trends. When it was discovered that PHP-CGI had poor performance, the FastCGI protocol happened to appear. So PHP implements a php version of FastCGI called PHPFPM (FastCGI Process Manager). When PHPFPM starts, it will start a master process and several work processes. The master process listens for requests and forwards them to the work process for processing. Each work process has a php interpreter. Your code has a copy in each work process. The work process is where the code is actually executed.

SO

PHPFPM listens to port 9000, nginx matches the php file and forwards the request to PHPFPM. After PHPFPM master listens to the request, it is assigned to work for processing (each work process has a php interpreter). When PHPFPM is started, the work process has already loaded the configuration and the code you wrote. Therefore, the work process executes the request immediately after receiving it, and then returns the result.

ngx_http_fastcgi_module module

The browser request web_server is http protocol or https protocol, but PHPFPM does not understand what should I do? Here nginx provides a ngx_http_fastcgi_module, which maps http or https requests to FastCGI requests. This way the php program can interact with the user.

Purely personal understanding, please point out if there is any mistake.

Related recommendations: [PHP Tutorial]

The above is the detailed content of How does PHP receive the request?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.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