什么是我的nginx + php服务器的瓶颈?

我在我的nginx服务器上运行一些攻击testing。 瓶颈似乎不是CPU或内存,那么它是什么?

我试图在我的MacBook上做到这一点:

sudo siege -t 10s -c 500 server_ip/test.php 

响应时间达到10秒,在完成之前我得到错误和围困中止。

但我如果在我的服务器上运行上述

 siege -t 10s -c 500 localhost/test.php 

我得到:

 Transactions: 6555 hits Availability: 95.14 % Elapsed time: 9.51 secs Data transferred: 117.30 MB Response time: 0.18 secs Transaction rate: 689.27 trans/sec Throughput: 12.33 MB/sec Concurrency: 127.11 Successful transactions: 6555 Failed transactions: 335 Longest transaction: 1.31 Shortest transaction: 0.00 

我还注意到,对于较低的并发数字,本地主机上的事务处理速度比外部处理大大提高了。

但是,当上面在本地主机上运行时,CPU使用率较低,HTOP上的内存使用率较低。 所以我很困惑如何提升性能,因为我看不到瓶颈。

ulimit返回50000,因为我增加了它。 有4个nginx工作进程是我的cpu核心的2倍。 这是我的其他设置

 worker_rlimit_nofile 40000; events { worker_connections 20000; # multi_accept on; } tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; 

test.php只是一个回声phpinfo()脚本,没有别的。 没有数据库连接。

这台机器是一个AWS m3大,2个cpu核心,我相信大约7gb的ram。

这是我的服务器块的内容:

  listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/sitename; index index.php index.html index.htm; # Make site accessible from http://localhost/ server_name localhost; location / { try_files $uri $uri.html $uri/ @extensionless-php; } location @extensionless-php { rewrite ^(.*)$ $1.php last; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } 

我的php-fpm设置:

  pm = dynamic ; The number of child processes to be created when pm is set to 'static' and the ; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'. ; This value sets the limit on the number of simultaneous requests that will be ; served. Equivalent to the ApacheMaxClients directive with mpm_prefork. ; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP ; CGI. The below defaults are based on a server without much resources. Don't ; forget to tweak pm.* to fit your needs. ; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand' ; Note: This value is mandatory. pm.max_children = 40 ; The number of child processes created on startup. ; Note: Used only when pm is set to 'dynamic' ; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2 pm.start_servers = 30 ; The desired minimum number of idle server processes. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' pm.min_spare_servers = 20 ; The desired maximum number of idle server processes. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' pm.max_spare_servers = 35 ; The number of seconds after which an idle process will be killed. ; Note: Used only when pm is set to 'ondemand' ; Default Value: 10s ;pm.process_idle_timeout = 10s; ; The number of requests each child process should execute before respawning. ; This can be useful to work around memory leaks in 3rd party libraries. For ; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS. ; Default Value: 0 ;pm.max_requests = 500 

500个并发请求对于这样一个小的PHP进程池太多了。 尝试提高你的最小和最大的PHP进程,并再次testing,直到达到最大的CPU使用率。

当然,如果你使用较低的客户端并发执行你的testing(可能是100,因为每个请求的CPU使用率很低),你将会获得更好的全局性能,而且没有请求错误。 同样的方法,如果你进行较长时间的testing,你应该观察到越来越差的统计数据和更高的错误率。

无论如何,一旦达到100%的CPU使用率testingscheme,请记住,当您部署生产代码时,这些池值将不会被使用。 一个典型的PHP电子商务应用程序的CPU密集度足以让您的2个vCPU挨饿,只需要4-5个并发签出。

通常,CPU或内存密集型应用程序可以更好地处理小型PHP进程池,而快速低CPU应用程序将更好地利用大型池。