Table of Contents
php-fpm Process Manager
php startup and working principle
php smooth restart principle
nginx working principle
Summary
Home Backend Development PHP Tutorial Introduction to the principle of nginx and php executing requests

Introduction to the principle of nginx and php executing requests

Feb 25, 2019 am 09:53 AM
nginx php

This article brings you an introduction to the principles of nginx and php execution requests. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

First of all, let’s understand the relationship between cgi, php-cgi, fastcgi and php-fpm that we often hear, and help us understand the working principle of php

cgi protocol

The cgi protocol is used to determine the webserver (such as nginx), that is, what data is passed by the content distribution server and what format the data is in.

php-cgi process interpreter

php-cgi is the cgi protocol process interpreter of php. Every time it is started, it needs to load the php.ini file -> initialize the execution environment -> process the request -> return content to the webserver -> ;php-cgi process exit process

fastcgi protocol

The fastcgi protocol is a supplement to the efficiency improvement of the cgi protocol, mainly for each request. It is necessary to start the optimization of a cgi interpreter process. It is no longer necessary for the cgi interpreter process 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 the implementation of the fastcgi protocol and is a process manager. It includes master and worker processes at startup. The master process listens on the port and receives requests from the webserver. There are generally multiple worker processes, each worker Each process has a cgi process interpreter, which is used to execute PHP code

php startup and working principle

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

php smooth restart principle

Every time after modifying the php.ini configuration and restarting, a new worker process will be started to load the new Configuration, and the previously existing process will be destroyed after the work is completed, so a smooth restart is achieved

nginx working principle

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

server {
    listen       80; #监听80端口,接收http请求
    server_name  www.example.com; #一般存放网址,表示配置的哪个项目
    root /home/wwwroot/zensmall/public/; # 存放代码的根目录地址或代码启动入口
    index index.php index.html; #网站默认首页
    
    #当请求网站的url进行location的前缀匹配且最长匹配字符串是该配置项时,按顺序检查文件是否存在,并返回第一个找到的文件
    location / {
          #try_files,按顺序检查文件是否存在,返回第一个找到的文件
          #$uri代表不带请求参数的当前地址
          #$query_string代表请求携带的参数
          try_files   $uri $uri/ /index.php?$query_string; #按顺序检查$uri文件,$uri地址是否存在,如果存在,返回第一个找到的文件;如果都不存在,发起访问/index.php?$query_string的内部请求,该请求会重新匹配到下面的location请求
    }
    
     #当请求网站的php文件的时候,反向代理到php-fpm去处理
    location ~ \.php$ {
          include       fastcgi_params; #引入fastcgi的配置文件
          fastcgi_pass   127.0.0.1:9000; #设置php fastcgi进程监听的IP地址和端口
          fastcgi_index  index.php; #设置首页文件
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #设置脚本文件请求的路径
    }
}
Copy after login

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

Summary

The following summarizes the simplest user request process:

User accesses the domain name->Domain name performs DNS resolution->Requests to the corresponding IP server and port->nginx Listen to the request of the corresponding port->nginx performs location matching on the URL->Execute the rules under the matching location->nginx forwards the request to php->php-fpm’s master process monitors the nginx request->master process Assign the request to one of the idle worker processes ->The worker process executes the request->The worker process returns the execution result to nginx->nginx returns the result to the user

The above is the detailed content of Introduction to the principle of nginx and php executing requests. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

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.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles