Home >Backend Development >PHP Tutorial >To communicate between nginx and php-fpm, should we use unix socket or TCP?

To communicate between nginx and php-fpm, should we use unix socket or TCP?

藏色散人
藏色散人forward
2019-12-31 13:28:344845browse

Preface

There are two communication methods between nginx and fastcgi, one is TCP method and the other is unix socket method. Both methods have their own advantages and disadvantages. Here are two configuration methods first, and then summarize the performance, security, etc.

TCP uses the TCP port to connect 127.0.0.1:9000

Socket uses the unix domain socket to connect the socket /dev/shm/PHP-cgi.sock (many tutorials use the path /tmp , and the path /dev/shm is tmpfs, which is much faster than the disk). When the server pressure is not high, there is not much difference between tcp and socket, but when the pressure is relatively high, the socket method has a real effect. Better.

Configuration Guide

1. TCP Configuration Method

TCP communication configuration is very simple and can be done in three steps

The first step is to edit /etc/nginx/conf.d/your site configuration file (if using the default configuration file, modify /etc/nginx/sites-available/default)

Change the fastcgi_pass parameter to 127.0.0.1:9000, like this:

location ~ \.php$ {
      index index.php index.html index.htm;
      include /etc/nginx/fastcgi_params;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi_params;
 }

The second step is to edit the php-fpm configuration file /etc/php5/fpm/pool.d/www.conf

Change the listen parameter to 127.0.0.1:9000, like this:

listen=127.0.0.1:9000

The third step, restart php-fpm, restart nginx

2. Unix socket configuration method

Unix socket should actually be called unix domain socket in a strict sense. It is a widely used method of inter-process communication (IPC) in *nix systems. Files (usually .sock) are used as the only socket. Identifier (descriptor), two processes that need to communicate can establish a channel for communication by referencing the same socket descriptor file.

Unix domain socket or IPC socket is a terminal that enables two or more processes on the same operating system to communicate data. In contrast to pipes, Unix domain sockets can use both byte streams and data queues, while pipe communication can only be through byte streams. The interface of Unix domain sockets is very similar to Internet sockets, but it does not use the underlying network protocol to communicate. The function of Unix domain socket is a component in the POSIX operating system. Unix domain sockets use the address of a system file as their identity. It can be referenced by system processes. So two processes can open a Unix domain sockets at the same time to communicate. However, this communication method occurs in the system kernel and does not propagate in the network.

Configuration requires five steps

The first step is to determine the storage location of your socket descriptor file.

can be placed anywhere in the system. If you want faster communication speed, you can place it under /dev/shm. This directory is the so-called tmpfs, which is an area that can be used directly by RAM. Therefore, read The writing speed will be very fast.

After deciding the file location, we need to modify the permissions of the file. Let nginx and php-fpm have read and write permissions on it. You can do this:

sudo touch /dev/shm/fpm-cgi.sock
sudo chown www-data:www-data /dev/shm/fpm-cgi.sock
sudo chmod 666 /dev/shm/fpm-cgi.sock

The second step is to modify php-fpm configuration file/etc/php5/fpm/pool.d/www.conf

Change the listen parameter to /dev/shm/fpm-cgi.sock, like this:

listen=/dev/shm/fpm-cgi.sock

Change the listen.backlog parameter to -1. The memory backlog is infinite. The default is 128. If the concurrency is high, an error will be reported.

 ; Set listen(2) backlog. A value of '-1' means unlimited.
 ; Default Value: 128 (-1 on FreeBSD and OpenBSD)
 listen.backlog = -1

The third step is to modify the nginx site configuration file.

Modify the fastcgi_pass parameter to unix:/dev/shm/fpm-cgi.sock, like this:

location~\.php${
      indexindex.phpindex.htmlindex.htm;
      include/etc/nginx/fastcgi_params;
      fastcgi_passunix:/dev/shm/fpm-cgi.sock;
      fastcgi_indexindex.php;
      includefastcgi_params;
}

The fourth step is to modify the /etc/sysctl.conf file to increase the number of concurrent connections at the kernel level

sudo echo'net.core.somaxconn = 2048'>>/etc/sysctl.conf
sudo sysctl-p

The fifth step, restart nginx and php-fpm services (it is best to restart php-fpm first and then restart nginx)

ps: If nginx does load balancing, don’t consider unix socket at all. The only way is to use TCP.

For more PHP related knowledge, please visit PHP Tutorial!

The above is the detailed content of To communicate between nginx and php-fpm, should we use unix socket or TCP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lukachen.com. If there is any infringement, please contact admin@php.cn delete