Home php教程 php手册 nginx 和 php-fpm 通信使用unix socket还是TCP,及其配置

nginx 和 php-fpm 通信使用unix socket还是TCP,及其配置

Jun 06, 2016 pm 08:09 PM
nginx php-fpm unix use communication

前言 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;
?}
Copy after login

?第二步,编辑php-fpm配置文件?/etc/php5/fpm/pool.d/www.conf

将listen参数修改为127.0.0.1:9000,像这样:

listen = 127.0.0.1:9000
Copy after login

?第三步,重启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
Copy after login

?第二步,修改php-fpm配置文件/etc/php5/fpm/pool.d/www.conf

将listen参数修改为/dev/shm/fpm-cgi.sock,像这样:

listen = /dev/shm/fpm-cgi.sock
Copy after login

?将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
Copy after login

?第三步,修改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;
}
Copy after login

第四步,修改/etc/sysctl.conf 文件,提高内核级别的并发连接数(这个系统级的配置文件我也不是特别熟悉,参考的是这篇博客:《Php-fpm TcpSocket vs UnixSocket》)

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

第五步, 重启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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to allow external network access to tomcat server How to allow external network access to tomcat server Apr 21, 2024 am 07:22 AM

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

New generation of optical fiber broadband technology - 50G PON New generation of optical fiber broadband technology - 50G PON Apr 20, 2024 pm 09:22 PM

In the previous article (link), Xiao Zaojun introduced the development history of broadband technology from ISDN, xDSL to 10GPON. Today, let’s talk about the upcoming new generation of optical fiber broadband technology-50GPON. █F5G and F5G-A Before introducing 50GPON, let’s talk about F5G and F5G-A. In February 2020, ETSI (European Telecommunications Standards Institute) promoted a fixed communication network technology system based on 10GPON+FTTR, Wi-Fi6, 200G optical transmission/aggregation, OXC and other technologies, and named it F5G. That is, the fifth generation fixed network communication technology (The5thgenerationFixednetworks). F5G is a fixed network

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Welcome to nginx!How to solve it? Welcome to nginx!How to solve it? Apr 17, 2024 am 05:12 AM

To solve the "Welcome to nginx!" error, you need to check the virtual host configuration, enable the virtual host, reload Nginx, if the virtual host configuration file cannot be found, create a default page and reload Nginx, then the error message will disappear and the website will be normal show.

How to deploy nodejs project to server How to deploy nodejs project to server Apr 21, 2024 am 04:40 AM

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

KDE Plasma 6.1 brings many enhancements to the popular Linux desktop KDE Plasma 6.1 brings many enhancements to the popular Linux desktop Jun 23, 2024 am 07:54 AM

After several pre-releases, the KDE Plasma development team unveiled version 6.0 of its desktop environment for Linux and BSD systems on 28 February, using the Qt6 framework for the first time. KDE Plasma 6.1 now comes with a number of new features t

How to generate URL from html file How to generate URL from html file Apr 21, 2024 pm 12:57 PM

Converting an HTML file to a URL requires a web server, which involves the following steps: Obtain a web server. Set up a web server. Upload HTML file. Create a domain name. Route the request.

The development history of wireless mice The development history of wireless mice Jun 12, 2024 pm 08:52 PM

Original title: "How does a wireless mouse become wireless?" 》Wireless mice have gradually become a standard feature of today’s office computers. From now on, we no longer have to drag long cords around. But, how does a wireless mouse work? Today we will learn about the development history of the No.1 wireless mouse. Did you know that the wireless mouse is now 40 years old? In 1984, Logitech developed the world's first wireless mouse, but this wireless mouse used infrared as a The signal carrier is said to look like the picture below, but later failed due to performance reasons. It was not until ten years later in 1994 that Logitech finally successfully developed a wireless mouse that works at 27MHz. This 27MHz frequency also became the wireless mouse for a long time.

See all articles