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!

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
