说明:高性能服务器通过配置nginx和php-fpm,代替docker容器技术,实现请求服务高并发处理。
主要说明配置主要参数,以及想过解释。
nginx主要配置
通过nginx实现php-fpm服务器负载,用户访问服务时,将请求分配给不同的php-fpm服务器。
# nginx启动worker进程数
worker_processes auto;
#Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes.定义进程可以打开的最大文件数,与linux内核相同即可
worker_rlimit_nofile 65535;
# 定义worker进程的优先级,数字越小优先级越高 [-20,19]
worker_priority -20;
events{
# The maximum number of connections that each worker process can handle simultaneously.每个进程同时处理的连接数
worker_connections 10240;
# If multi_accept is disabled, a worker process will accept one new connection at a time. Otherwise, a worker process will accept all new connections at a time.
multi_accept on;
}
http{
access_log /var/log/nginx/access.log main buffer=32k;
# Nginx将使用sendfile内核来调用处理文件传递。
sendfile on;
# 根据权重分配请求到不同服务器,以下配置,当有6个请求时,5个发送到9000端口服务器,1个发送到9001端口服务器
upstream phpload{
server 127.0.0.1:9000 weight=6;
server 127.0.0.1:9001 weight=1;
}
server{
listen 443;
root /data/www/webserver;
index index.php;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ .php($|/){
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+.php)(/.+)") {
set $script $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$script;
fastcgi_param SCRIPT_NAME $script;
fastcgi_param PATH_INFO $path_info;
try_files $uri =404;
# 使用负载,分发请求到上游php服务器
fastcgi_pass myfastcgi;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
php-fpm主要配置
- pm为dynamic配置时,php-fpm最大启动100子线程时,根据测试结果性能最好。
- max_requests 设置获取请求数量尽可能大
; php服务器监听端口号
listen = 127.0.0.1:9001
pm = dynamic
pm.max_children = 100
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 40
; php-fpm工作进程处理完多少请求后自动重启
pm.max_requests = 10240
php-fpm实例启动
php-fpm启动实例,设置不同的配置文件,每个配置文件监听不同端口号
php-fpm -y /etc/php-fpm.d/9001.conf
php-fpm -h 查看帮助,获取更多命令使用方式
php-fpm操作命令
PID为php-fpm实例master进程号
重新加载实例配置
kill -USR2 [PID]
从容停止php-fpm
kill -QUIT [PID]
测试
配置:4核CPU8G内存20M带宽,开启3个php-fpm,每个php-fpm最大启动150子线程,nginx启动4个worker,php版本5.4
ab -c 200 -n 10000 https://tongkuaikeji.com/
结果
CPU使用率100%,内存使用率20%,带宽峰值11M,内存使用率和带宽峰值与后台服务运行数据和传输数据相关,仅供参考
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking tongkuaikeji.com (be patient)
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.14.2
Server Hostname: tongkuaikeji.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
Document Path: /
Document Length: 1662 bytes
Concurrency Level: 200
Time taken for tests: 56.434 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 20050000 bytes
HTML transferred: 16620000 bytes
Requests per second: 177.20 [#/sec] (mean)
Time per request: 1128.685 [ms] (mean)
Time per request: 5.643 [ms] (mean, across all concurrent requests)
Transfer rate: 346.95 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 81 246 445.6 112 8881
Processing: 403 849 269.5 837 2120
Waiting: 403 848 269.0 835 2120
Total: 493 1095 452.8 1029 9311
Percentage of the requests served within a certain time (ms)
50% 1029
66% 1135
75% 1214
80% 1283
90% 1551
95% 1725
98% 2127
99% 2598
100% 9311 (longest request)
版权声明:本文为u011385445原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。