are Nginx and PHP-FPM using a TCP socket or a UNIX socket?

Source: Internet
Author: User
Tags fpm unix domain socket nginx server
TCP Sockets

Allows communication between processes through a network, or through loopback, to communicate between local processes. UNIX Sockets

Allows communication between processes that are running locally. Analysis


As you can see from the picture above, the UNIX socket reduces the unnecessary TCP overhead, and TCP needs to go through the loopback and request temporary ports and TCP-related resources. However, the UNIX socket high concurrency is unstable, when the number of connections occurs, there will be a large number of long time caching, in the absence of a connection-oriented protocol support, large packets may be directly error does not return an exception. The connection-oriented protocol such as TCP can guarantee the correctness and completeness of communication. my choice.

If you are running on the same server nginx and PHP-FPM, concurrency is not more than 1000, select the UNIX socket, because it is local, you can avoid some check operations (routing, etc.), so faster, lighter.
If I am facing a high concurrency business, I will choose to use more reliable TCP sockets to maintain efficiency with load balancing, kernel optimization, and other operational tools. Nginx and php-fpm using UNIX sockets

Put the sock file in the/DEV/SHM directory and use more memory to read and write faster.

# CD/DEV/SHM Touch
php7.0-fpm.sock 
chown www-data:www-data php7.0-fpm.sock chmod 777
Php7.0-fpm.sock

PHP-FPM Configuration

# vi/etc/php/7.0/fpm/pool.d/www.conf
listen=/dev/shm/php7.0-fpm.sock
listen.owner = Www-data
Listen.group = Www-data

Nginx Server Block Configuration

  Location ~* \.php$ {
    fastcgi_pass unix:/dev/shm/php7.0-fpm.sock;
    Fastcgi_param script_filename $document _root$fastcgi_script_name;
    Include/etc/nginx/fastcgi_params;
  }
improve UNIX socket stability for Nginx and PHP-FPM (limited stand-alone capability)

1. Modify Kernel parameters

Net.unix.max_dgram_qlen = 4096
Net.core.netdev_max_backlog = 4096
Net.core.somaxconn = 4096

2. Improve backlog
Backlog default bit 128,1024 This value is best converted to its own normal QPS.

nginx.conf
server{
  listen default backlog=1024;
}

php-fpm.conf
Listen.backlog = 1024

3. Add sock documents and PHP-FPM examples
Create a new sock file in/dev/shm, and in Nginx the request load is balanced to two sock files through upstream magic resistance,
And the two sock files are mapped to two sets of PHP-FPM instances respectively. Reference

Nginx through TCP and Unix-domain-socket connection fastcgi mode comparison
Nginx, php-fpm default configuration and performance –tcp socket or UNIX domain socket

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.