How does PHP receive the request?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
