


To communicate between nginx and php-fpm, should we use unix socket or TCP?
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

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.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

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.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

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

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".
