Home Backend Development PHP Tutorial How does nginx+php perform requests? Introduction to working principle

How does nginx+php perform requests? Introduction to working principle

Feb 26, 2019 am 10:36 AM
nginx php

nginx How does php perform requests? The following article will introduce to you the working principle of nginx php execution request. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

php working principle

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

cgi protocol

cgi protocol is used to determine the webserver (such as nginx), which is the content distribution server delivery What data is coming and what format of data

php-cgi process interpreter

php-cgi is the cgi protocol process interpreter of php , each time you start, you need to go through the process of loading the php.ini file -> initializing the execution environment -> processing the request -> returning content to the webserver ->php-cgi process exit

fastcgi protocol

The fastcgi protocol is a supplement to the efficiency improvement of the cgi protocol. It is mainly optimized for the need to start a cgi interpreter process every time a request comes. It is no longer needed. The cgi interpreter process 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 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, and each worker process There is a cgi process interpreter used to execute PHP code

php startup and working principle

When starting phpfpm, the master process will be started. Load the php.ini file, initialize the execution environment, 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 the php.ini configuration is modified After restarting, 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

nginx working principle

If you want to understand the principle of cooperation between nginx and php, you also need to first understand 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 Monitor the URL request on port 80 and 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-> ;Request to the corresponding IP server and port->nginx listens 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 The master process listens to the nginx request->the master process assigns 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 entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How does nginx+php perform requests? Introduction to working principle. 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)

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.

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.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

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: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

See all articles