Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure the communication mechanism between Nginx and php-fpm

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to configure the communication mechanism between Nginx and php-fpm, which is very detailed and has a certain reference value. Interested friends must read it!

PHP-FPM introduces CGI protocol and FastCGI protocol

The code file of each dynamic language (PHP,Python, etc.) can only be recognized by the server through the corresponding parser, and the CGI protocol is used to enable the interpreter and the server to communicate with each other. The parsing of PHP files on the server needs to use the PHP interpreter, coupled with the corresponding CGI protocol, so that the server can parse to PHP files.

Because the mechanism of CGI is to fork a CGI process for each request, kill the process after the request ends, which is a waste of resources in practical application, so there is an improved version of CGI, FastCGI,FastCGI, after the request is processed, will not kill the process, but continue to process multiple requests, which greatly improves the efficiency.

What is PHP-FPM?

PHP-FPM is PHP-FastCGI Process Manager, which is the implementation of FastCGI and provides the function of process management. Processes include master processes and worker processes; there is only one master process, which is responsible for listening on the port and receiving requests from the server, while there are generally multiple worker processes (the specific number is configured according to the actual needs), and each process is embedded with a PHP interpreter, which is where the code is really executed.

Communication mechanism between Nginx and php-fpm

When we visit a website (such as www.test.com), the process goes like this:

Www.test.com | | Nginx | | Route to www.test.com/index.php | | load fast-cgi module of nginx | | fast-cgi listens to 127.0.0.1 nginx 9000 address | | www.test.com/index.php request arrives at 127.0.0.1 www.test.com/index.php 9000 | | waiting for processing. Combination of Nginx and php-fpm

On Linux, nginx communicates with php-fpm in two ways: tcp socket and unix socket.

The advantage of tcp socket is that it can be used across servers, which can only be used when nginx and php-fpm are not on the same machine.

Unix socket, also known as IPC (inter-process communication Inter-process Communication) socket, is used to achieve inter-process communication on the same host. This way requires filling in the socket file location of php-fpm in the nginx configuration file.

The data transfer process of the two modes is shown in the following figure:

The difference between the two:

Because Unix socket does not need to go through the network protocol stack, it does not need to pack and unpack, calculate checksum, maintain sequence number and reply, etc., it just copies application layer data from one process to another. Therefore, it is more efficient than tcp socket, and can reduce unnecessary tcp overhead. However, unix socket is unstable when the concurrency is high, and a large number of long-term caches will be generated when the number of connections breaks out. Without the support of the connection-oriented protocol, big data package may make a direct error and do not return an exception. Connection-oriented protocols such as tcp can better ensure the correctness and integrity of communication.

To combine Nginx with php-fpm, you only need to make settings in their respective configuration files:

1) configuration in Nginx

Take tcp socket communication as an example

Server {listen 80; # listens to port 80 to receive http requests server_name www.test.com; # is the site address root / usr/local/etc/nginx/www/huxintong_admin; # the path to store the code project # the processing location / {index index.php; # jumps to www.test.com/index.php autoindex on when routed to the site root directory location } # when requesting the php file under the website, reverse proxy to php-fpm location ~\ .php$ {include / usr/local/etc/nginx/fastcgi.conf; # load nginx's fastcgi module fastcgi_intercept_errors on; fastcgi_pass 127.0.0.1 include 9000 # tcp method, IP address and port of php-fpm listener # fasrcgi_pass / usr/run/php-fpm.sock # unix socket connection}}

2) configuration of php-fpm

Listen = 127.0.0.1 listen 900cycles or the following listen = / var/run/php-fpm.sock Note: when connecting with unix socket, because the socket file is essentially a file, there is a problem of permission control, so you need to pay attention to the permissions of the nginx process and php-fpm, otherwise you will be prompted to have no permission to access. (set up users in their respective profiles)

Through the above configuration, the communication between php-fpm and nginx can be completed.

Choice in application

If nginx and php-fpm are running on the same server, and the concurrency is not more than 1000, select unix socket to improve the communication efficiency of nginx and php-fpm.

If you are faced with high concurrency business, consider using a more reliable tcp socket to maintain efficiency by operation and maintenance means such as load balancing and kernel optimization.

If the concurrency is high but still want to use unix socket, you can improve the stability of unix socket in the following ways.

1) put the sock file in the / dev/shm directory, and put the sock file in memory in this directory, so that the memory can read and write faster.

2) improve backlog

The default value of 128j024 of backlog is best converted to its own normal QPS, which is configured as follows.

In the nginx.conf file

Server {listen 80 default backlog = 1024;}

In the php-fpm.conf file

Listen.backlog = 1024

3) add sock files and php-fpm instances

Create a new sock file in / dev/shm, balance the request load to two sock files through the upstream module in nginx, and correspond the two sock files to two sets of php-fpm instances respectively.

These are all the contents of the article "how to configure the communication mechanism between Nginx and php-fpm". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report