Original: http://blog.csdn.net/pcyph/article/details/46513521
--------------------------------------------------------------------------------------------------------------- -----
Objective
There are two ways of communication between Nginx and FastCGI, one is TCP, and the other is UNIX Socke mode. There are pros and cons in both ways, here are two ways to configure the configuration, and then the performance, security and so on to make a summary.
TCP is connected using TCP ports 127.0.0.1:9000
Sockets are socket/dev/shm/php-cgi.sock using UNIX domain sockets (many tutorials use the path/tmp, and Path/dev/shm is a tmpfs, much faster than disk), and when the server is under a lot of pressure, TCP and socket are not very different, but when the pressure is full, using socket, the effect is really better.
Configuration Guide
How TCP is configured
TCP communication is easy to configure and can be done in three steps.
The first step, edit/etc/nginx/conf.d/your site profile (if using the default profile, modify/etc/nginx/sites-available/default)
Modify the Fastcgi_pass parameter to 127.0.0.1:9000, like this:
1234567 |
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, edit the php-fpm configuration file/etc/php5/fpm/pool.d/www.conf
Modify the Listen parameter to 127.0.0.1:9000, like this:
Step three, restart PHP-FPM, restart Nginx
Unix Socket Configuration method
UNIX sockets In fact should be called UNIX domain socket, it is *nix system interprocess communication (IPC) is a widely used way, With a file (typically. sock) as the unique identifier (descriptor) of the socket, the two processes that need to communicate refer to the same socket descriptor file to establish a channel for communication.
A Unix domain socket or IPC socket is a terminal that enables two or more processes on the same operating system to communicate data. Unix domain sockets can use both byte stream and data queue, while pipeline communication is only possible through byte stream than pipeline. The Unix domain sockets interface is similar to an Internet socket, but it does not use the network underlying protocol to communicate. The Unix domain socket feature is a component of the POSIX operating system. Unix domain sockets uses the address of the system file as its own identity. It can be referenced by the system process. So two processes can open a UNIX domain sockets at the same time to communicate. However, this mode of communication occurs in the kernel of the system and does not propagate across the network.
Configuration takes five steps
The first step is to determine where your socket descriptor file is stored.
Can be placed anywhere in the system, if you want faster communication speed, can be placed under the/DEV/SHM, this directory is so-called TMPFS, RAM can be used directly in the area, so, read and write speed will be fast.
Determine the file location, you need to modify the permissions of the file, to let Nginx and PHP-FPM have read and write permissions, you can:
123 |
sudo touch /dev/shm/ fpm-cgi.sock sudo chown www-data:www- data /dev/< Span class= "crayon-v" >shm/fpm-cgi. Sock sudo chmod 666 /dev/shm/fpm-CGI. Sock |
The second step is to modify the PHP-FPM configuration file/etc/php5/fpm/pool.d/www.conf
Modify the Listen parameter to/dev/shm/fpm-cgi.sock, like this:
1 |
Listen=/dev/shm/fpm-CGI. Sock |
Change the Listen.backlog parameter to-1, the memory backlog is infinite, the default is 128, the concurrency is high and then will be an error
123 |
; Set Listen(2) backlog. A value of '-1 ' means unlimited. ; Default Value: (-1 on FreeBSD and OpenBSD) Listen. Backlog = -1 |
Step three, modify the Nginx site configuration file
Modify the Fastcgi_pass parameter to Unix:/dev/shm/fpm-cgi.sock, like this:
1234567 |
location~\. PHP${ Indexindex. PHPindex. HTMLindex. HTM; include/etc/nginx/fastcgi_params; fastcgi_passUnix:/dev/shm/fpm-CGI. Sock; fastcgi_indexindex. PHP; includefastcgi_params; } |
Fourth step, modify the/etc/sysctl.conf file, increase the number of concurrent connections at the kernel level (this system-level configuration file I am not particularly familiar with, referring to this blog: "php-fpm tcpsocket vs Unixsocket")
12 |
sudoecho' net.core.somaxconn = 2048 '>>/etc/sysctl. confsudosysctl-p |
Fifth step, restart Nginx and PHP-FPM service (preferably restart PHP-FPM and restart Nginx)
Analysis and summary of two kinds of communication methods
In principle, the UNIX socket is definitely faster and consumes less resources than TCP, because the sockets communicate between the Nginx and PHP-FPM processes, and TCP needs to go through a local loopback drive, as well as request temporary ports and TCP-related resources.
Of course, the UNIX socket is not so stable in principle, and when the number of concurrent connections bursts, it produces a lot of long-time caches, and in the absence of a connection-oriented protocol support, large packets are likely to be directly faulted and will not return an exception. However, the connection-oriented protocol such as TCP can guarantee the correctness and completeness of the communication.
Of course, the above is a semi-understanding of the theoretical analysis plus subjective speculation, the specific difference or to pass the test data to speak, and later, will do this test. My theoretical analysis of the test data from other bloggers on the internet is almost right. As for which way you choose, I can only say "fish and bear paw can not have both", through the superb transport and configuration skills, the performance and stability of a balance.
Tell me about my choice.
In fact, if nginx do to do load balancing, do not consider the UNIX socket way, can only adopt the TCP way. Now my station is not so high concurrency, so the UNIX socket, and later if there is a high concurrency business, and then some parameter adjustment can be dealt with, if really can not support, that can only do load balance, then naturally will choose TCP mode.
"Go" nginx and PHP-FPM communication using UNIX sockets or TCP, and their configuration