nginx 和 php-fpm 使用unix socket配置比原来的fastcgi_pass 127.0.0.1:9000速度快一倍
测试环境是单核2GB内存的机器,请求数量10000次,并发100 PHP脚本直接输出hello world平均测试QPS是5000/S左右
创建/tmp/php-fpm.sock文件
touch /tmp/php-fpm.sock
chmod 0777 /tmp/php-fpm.sock 给文件权限
PHP配置unix socket:
cd /usr/local/php/etc/php-fpm.d
vim www.conf (我的配置文件是www.conf)
;listen = 127.0.0.1:9000 (默认是这样的)
listen = /tmp/php-fpm.sock
然后启动PHP /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini
nginx配置unix socket:
在nginx的配置文件server段
location ~ .php(.*)$ {
root /usr/local/nginx/html/xxx;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
重启nginx
测压:ab -n 10000 -c 1000 http://localhost/hello.php
Server Software: nginx
Server Hostname: localhost
Server Port: 80
Document Path: /hello.php
Document Length: 11 bytes
Concurrency Level: 1000
Time taken for tests: 1.932 seconds
Complete requests: 10000
Failed requests: 2204
(Connect: 0, Receive: 0, Length: 2204, Exceptions: 0)
Write errors: 0
Non-2xx responses: 2204
Total transferred: 1963988 bytes
HTML transferred: 451620 bytes
Requests per second: 5175.22 [#/sec] (mean)
Time per request: 193.228 [ms] (mean)
Time per request: 0.193 [ms] (mean, across all concurrent requests)
Transfer rate: 992.58 [Kbytes/sec] received
之前用 fastcgi_pass 127.0.0.1:9000 TCP协议跟nginx通讯的情况,QPS只有2500这样,单机的情况下强烈推荐用unix socket 跟nginx通讯,如果php在其他机器上,那只能走ip端口的TCP协议了。