nginx 中fastcgi_pass监听unix socket和tcp socket的区别

本文详细介绍了Nginx与PHP-FPM之间的两种通信方式:TCPSocket和UNIX Domain Socket,并对比了它们在不同并发场景下的性能表现。此外,还提供了使用UNIX Domain Socket的具体配置步骤及优化建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx和PHP-FPM的进程间通信有两种方式,一种是TCP Socket,一种是UNIX Domain Socket.
其中TCP Socket是IP加端口,nginx默认的通信方式,可以跨服务器,非常适合做负载均衡.而UNIX Domain Socket是发生在系统内核里而不经过网络,只能用于Nginx跟PHP-FPM都在同一服务器的场景.

通信流程如下:
UNIX Domain Socket:
Nginx <=> socket <=> PHP-FPM
TCP Socket(本地回环):
Nginx <=> socket <=> TCP/IP <=> socket <=> PHP-FPM
TCP Socket(Nginx和PHP-FPM位于不同服务器):
Nginx <=> socket <=> TCP/IP <=> 物理层 <=> 路由器 <=> 物理层 <=> TCP/IP <=> socket <=> PHP-FPM


Mysql命令行客户端连接mysqld服务也类似有这两种方式:
使用Unix Socket连接(默认):
mysql -uroot -p --protocol=socket --socket=/tmp/mysql.sock
使用TCP连接:
mysql -uroot -p --protocol=tcp --host=127.0.0.1 --port=3306

看过很多帖子结论是
理论上UNIX Domain Socket要比TCP Socket高效,实际上是:
当访问压力较小(每秒并发不超过1w)时,Unix domain socket和TCP socket,在性能上没有显著差别(也能说Unixsock稍好)。
当高并发的时,Tcp Socket能表现出更高的稳定性,且性能优于Unix Socket,缺点是会占用大量的临时端口,需要用内核参数优化。

用UNIX Domain Socket的配置方法:

centos 6.5 x86_64,php5.3
一:首先建立/dev/shm/php-cgi.sock文件,然后将之改所有者改为nginx(与 nginx的用户一致):

#touch /dev/shm/php-cgi.sock     (/dev/shm是内存文件系统,socket放在内存中肯定会快些)

#chown nginx:nginx /dev/shm/php-cgi.sock

二:vi /etc/nginx/conf.d/default.conf

修改:
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;

三:vi /etc/php-fpm.d/www.conf中修改配置为:

;listen = 127.0.0.1:9000
listen = /dev/shm/php-cgi.sock

四:重启服务

service php-fpm restart
service nginx restart


ls -all查看/dev/shm/php-cgi.sock由普通文件变成s开头的unix套接字

需要优化的参数:
1,增加文件打开数
echo ‘ulimit -HSn 65535′ >> /etc/profile
echo ‘ulimit -HSn 65535′ >> /etc/rc.local
source /etc/profile

2,增加php-fpm打开文件描述符限制
vi /etc/php-fpm.d/www.conf
rlimit_files = 51200

3,调高内核socket最大连接数
echo 'net.core.somaxconn = 2048' >> /etc/sysctl.conf   
sysctl -p 

4,调高nginx和php-fpm中的backlog
在nginx配置文件/etc/nginx/conf.d/default.conf中这个域名的server下,在listen 80后面添加default backlog=2048。同时配置/etc/php-fpm.d/www.conf中的listen.backlog为2048,默认为128。

5,增加sock文件和php-fpm实例数
再新建一个sock文件,在Nginx中通过upstream模块将请求负载均衡到两个sock文件背后的两套php-fpm实例上。

6.php-fpm调优(双核2G内存)
sed -i 's/;request_terminate_timeout = 0/request_terminate_timeout = 0/g' /etc/php-fpm.d/www.conf

sed -i 's/pm.max_children = 50/pm.max_children = 100/g' /etc/php-fpm.d/www.conf

sed -i 's/pm.start_servers = 5/pm.start_servers = 10/g' /etc/php-fpm.d/www.conf

sed -i 's/pm.min_spare_servers = 5/pm.min_spare_servers = 10/g' /etc/php-fpm.d/www.conf

sed -i 's/pm.max_spare_servers = 35/pm.max_spare_servers = 70/g' /etc/php-fpm.d/www.conf

sed -i 's/;pm.max_requests = 500/pm.max_requests = 1024/g' /etc/php-fpm.d/www.conf

sed -i 's/;rlimit_files = 1024/rlimit_files = 65535/g' /etc/php-fpm.d/www.conf

service php-fpm start

 

 

转载于:https://my.oschina.net/u/2404183/blog/716927

# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80; listen [::]:80; server_name _; autoindex on; root /usr/share/nginx/html; index index.php index.html; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params; } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2; # listen [::]:443 ssl http2; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } }
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值