nginx 和 php-fpm 通信使用unix socket还是TCP,及其配置
前言 nginx和fastcgi的通信方式有两种,一种是TCP的方式,一种是unix socke方式。两种方式各有优缺点,这里先给出两种的配置方法,然后再对性能、安全性等做出总结。 配置指南 TCP配置方式 TCP通信配置起来很简单,三步即可搞定 第一步 ,编辑 /etc/nginx/co
前言
nginx和fastcgi的通信方式有两种,一种是TCP的方式,一种是unix socke方式。两种方式各有优缺点,这里先给出两种的配置方法,然后再对性能、安全性等做出总结。
配置指南
TCP配置方式
TCP通信配置起来很简单,三步即可搞定
第一步,编辑 /etc/nginx/conf.d/你的站点配置文件(如果使用的默认配置文件,修改/etc/nginx/sites-available/default)
将fastcgi_pass参数修改为127.0.0.1:9000,像这样:
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; ?}
?第二步,编辑php-fpm配置文件?/etc/php5/fpm/pool.d/www.conf
将listen参数修改为127.0.0.1:9000,像这样:
listen = 127.0.0.1:9000
?第三步,重启php-fpm,重启nginx
unix socket配置方式
unix socket其实严格意义上应该叫unix domain socket,它是*nix系统进程间通信(IPC)的一种被广泛采用方式,以文件(一般是.sock)作为socket的唯一标识(描述符),需要通信的两个进程引用同一个socket描述符文件就可以建立通道进行通信了。
配置需要五步
第一步,决定你的socket描述符文件的存储位置。
可以放在系统的任意位置,如果想要更快的通信速度,可以放在/dev/shm下面,这个目录是所谓的tmpfs,是RAM可以直接使用的区域,所以,读写速度都会很快。
决定了文件位置,就要修改文件的权限了,要让nginx和php-fpm对它都有读写的权限,可以这样:
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
?第二步,修改php-fpm配置文件/etc/php5/fpm/pool.d/www.conf
将listen参数修改为/dev/shm/fpm-cgi.sock,像这样:
listen = /dev/shm/fpm-cgi.sock
?将listen.backlog参数改为-1,内存积压无限大,默认是128,并发高了之后就会报错
?; Set listen(2) backlog. A value of '-1' means unlimited. ?; Default Value: 128 (-1 on FreeBSD and OpenBSD) ?listen.backlog = -1
?第三步,修改nginx站点配置文件
将fastcgi_pass参数修改为unix:/dev/shm/fpm-cgi.sock,像这样:
location ~ \.php$ { index index.php index.html index.htm; include /etc/nginx/fastcgi_params; fastcgi_pass unix:/dev/shm/fpm-cgi.sock; fastcgi_index index.php; include fastcgi_params; }
第四步,修改/etc/sysctl.conf 文件,提高内核级别的并发连接数(这个系统级的配置文件我也不是特别熟悉,参考的是这篇博客:《Php-fpm TcpSocket vs UnixSocket》)
sudo echo 'net.core.somaxconn = 2048' >> /etc/sysctl.conf sudo sysctl -p
第五步, 重启nginx和php-fpm服务(最好先重启php-fpm再重启nginx)
两种通信方式的分析和总结
从原理上来说,unix socket方式肯定要比tcp的方式快而且消耗资源少,因为socket之间在nginx和php-fpm的进程之间通信,而tcp需要经过本地回环驱动,还要申请临时端口和tcp相关资源。
当然还是从原理上来说,unix socket会显得不是那么稳定,当并发连接数爆发时,会产生大量的长时缓存,在没有面向连接协议支撑的情况下,大数据包很有可能就直接出错并不会返回异常。而TCP这样的面向连接的协议,多少可以保证通信的正确性和完整性。
当然以上主要是半懂不懂的理论分析加主观臆测,具体的差别还是要通过测试数据来说话,以后有空,会进行这方面的测试。从网上别人博客的测试数据,我的理论分析差不多是对的。至于你选择哪种方式,我只能说“鱼和熊掌不可兼得也”,通过高超的运维和配置技巧,在性能和稳定性上做一个平衡吧。
说说我的选择
其实,如果nginx做要做负载均衡的话,根本也不要考虑unix socket的方式了,只能采用TCP的方式。现在我的小站没有那么高的并发量,所以就用unix socket了,以后如果有了高并发业务,再进行一些参数调整即可应付,如果真要是无法支撑,那只能做负载均衡了,到时候自然会选择TCP方式。
声明:如未作说明,则本文为 渣滓洞【解旻的博客】 原创。转载务必注明出处。
注意:转载须保留全文,如需修改请联系作者。
本文永久地址:http://xieminis.me/?p=216

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











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.

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".

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 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.

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]

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.

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.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
