请帮我调整我的configuration为php5-fpm和nginx。
问题是我的php5-fpm日志不断报告缓慢的脚本和杀死一个子线程。
专用服务器,四核xeon,32Gb Ram。 1 php应用程序/网站运行。
Php应用程序:果壳,search引擎,结果消防curl请求。 页面加载时间通常是每个search2-3秒。
这是我认为正在发生的事情我得到750个并发的php用户执行search。 由于内存限制,我只能设置pm.max_children = 400。 我假设每个用户50Mb(子线程),使得= 20GB。 我假设每个用户= 1个子线程。 所以,pm.max_children不足以覆盖正在进行search需要3秒钟的750个活跃的php用户。
所以我认为我看到用户排队,因为我看到3秒变成4 – 7秒。 作为用户排队,我认为脚本越来越慢触发错误日志消息和php5-fpm下午杀死孩子?
这是我认为正在发生的事情。 我在下面提供了我的错误日志输出,nginx,php5-fpm config。
我真的很感激任何意见,如果我可以调整我的configuration,如果pm.max_children应该至less等于你最大并发用户,请记住,我的PHPsearch打开约3秒。 我是否需要更多的内存或更多的服务器?
这是我的记忆,但是我只在30分钟前重启了nginx
:/var/log# free -m total used free shared buffers cached Mem: 32151 26175 5975 0 186 13334 -/+ buffers/cache: 12654 19496 Swap: 32739 5 32734
php5-fpm:www.conf:进程pipe理器被设置为静态
我使用静态的,因为我认为所有的孩子将立即可用,而不是产卵时间,我只在箱子上运行1个应用程序。
;pm = dynamic pm = static ;pm.max_children = 10 pm.max_children = 400 ;pm.start_servers = 4 pm.start_servers = 150 ;pm.min_spare_servers = 2 pm.min_spare_servers = 32 ;pm.max_spare_servers = 6 pm.max_spare_servers = 64 ;pm.max_requests = 500 pm.max_requests = 10000
php5-fpm日志中的错误
我应该澄清一下,我在高负载下看到的行为,750个用户一次就是,caching的search结果并没有caching开始需要更长的时间。 即对于高速caching大于1秒,对于非高速caching在4和7秒之间。 所以,当用户排队等待,我认为search时间增加,并螺旋到脚本在负载下运行缓慢的地步。 触发通知,孩子死亡。
比如刚刚重启之后
[04-Jun-2013 20:11:07] NOTICE: Finishing ... [04-Jun-2013 20:11:11] NOTICE: exiting, bye-bye! [04-Jun-2013 20:11:12] NOTICE: fpm is running, pid 17899 [04-Jun-2013 20:11:12] NOTICE: ready to handle connections [04-Jun-2013 20:27:28] WARNING: [pool www] child 18200, script '/home/site/public_html/index.php' (request: "POST /index.php") executing too slow (10.827363 sec), logging [04-Jun-2013 20:27:28] WARNING: [pool www] child 18138, script '/home/site/public_html/index.php' (request: "POST /index.php") executing too slow (10.827034 sec), logging [04-Jun-2013 20:27:28] NOTICE: child 18138 stopped for tracing [04-Jun-2013 20:27:28] NOTICE: about to trace 18138 [04-Jun-2013 20:27:28] NOTICE: finished trace of 18138 [04-Jun-2013 20:27:28] NOTICE: child 18200 stopped for tracing [04-Jun-2013 20:27:28] NOTICE: about to trace 18200 [04-Jun-2013 20:27:28] NOTICE: finished trace of 18200 [04-Jun-2013 20:52:52] WARNING: [pool www] child 17948, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (11.724081 sec), logging [04-Jun-2013 20:52:52] NOTICE: child 17948 stopped for tracing [04-Jun-2013 20:52:52] NOTICE: about to trace 17948 [04-Jun-2013 20:52:52] ERROR: failed to ptrace(PEEKDATA) pid 17948: Input/output error (5) [04-Jun-2013 20:52:52] NOTICE: finished trace of 17948 [04-Jun-2013 20:58:22] WARNING: [pool www] child 18287, script '/home/site/public_html/index.php' (request: "POST /index.php") executing too slow (10.701504 sec), logging [04-Jun-2013 20:58:22] NOTICE: child 18287 stopped for tracing [04-Jun-2013 20:58:22] NOTICE: about to trace 18287 [04-Jun-2013 20:58:22] NOTICE: finished trace of 18287 [04-Jun-2013 21:19:22] WARNING: [pool www] child 18224, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (10.005466 sec), logging [04-Jun-2013 21:19:22] WARNING: [pool www] child 18197, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (12.141221 sec), logging [04-Jun-2013 21:19:22] WARNING: [pool www] child 17946, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (11.107080 sec), logging [04-Jun-2013 21:19:22] NOTICE: child 17946 stopped for tracing [04-Jun-2013 21:19:22] NOTICE: about to trace 17946 [04-Jun-2013 21:19:22] NOTICE: finished trace of 17946 [04-Jun-2013 21:19:22] NOTICE: child 18197 stopped for tracing [04-Jun-2013 21:19:22] NOTICE: about to trace 18197 [04-Jun-2013 21:19:22] NOTICE: finished trace of 18197 [04-Jun-2013 21:19:22] NOTICE: child 18224 stopped for tracing [04-Jun-2013 21:19:22] NOTICE: about to trace 18224 [04-Jun-2013 21:19:22] NOTICE: finished trace of 18224 [04-Jun-2013 21:19:26] WARNING: [pool www] child 18197, script '/home/site/public_html/index.php' (request: "GET /index.php") execution timed out (15.475021 sec), terminating [04-Jun-2013 21:19:26] WARNING: [pool www] child 18055, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (12.927407 sec), logging [04-Jun-2013 21:19:26] NOTICE: child 18055 stopped for tracing [04-Jun-2013 21:19:26] NOTICE: about to trace 18055 [04-Jun-2013 21:19:26] NOTICE: finished trace of 18055 [04-Jun-2013 21:19:26] WARNING: [pool www] child 18197 exited on signal 15 (SIGTERM) after 4094.193190 seconds from start [04-Jun-2013 21:19:26] NOTICE: [pool www] child 5137 started [04-Jun-2013 21:24:49] WARNING: [pool www] child 17918, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (11.367854 sec), logging [04-Jun-2013 21:24:49] NOTICE: child 17918 stopped for tracing [04-Jun-2013 21:24:49] NOTICE: about to trace 17918 [04-Jun-2013 21:24:49] NOTICE: finished trace of 17918 [04-Jun-2013 21:24:53] WARNING: [pool www] child 18226, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (10.763667 sec), logging [04-Jun-2013 21:24:53] WARNING: [pool www] child 18206, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (12.060464 sec), logging [04-Jun-2013 21:24:53] WARNING: [pool www] child 18073, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (11.846097 sec), logging [04-Jun-2013 21:24:53] NOTICE: child 18073 stopped for tracing [04-Jun-2013 21:24:53] NOTICE: about to trace 18073 [04-Jun-2013 21:24:53] NOTICE: finished trace of 18073 [04-Jun-2013 21:24:53] NOTICE: child 18206 stopped for tracing [04-Jun-2013 21:24:53] NOTICE: about to trace 18206 [04-Jun-2013 21:24:53] NOTICE: finished trace of 18206 [04-Jun-2013 21:24:53] NOTICE: child 18226 stopped for tracing [04-Jun-2013 21:24:53] NOTICE: about to trace 18226 [04-Jun-2013 21:24:53] NOTICE: finished trace of 18226 [04-Jun-2013 21:24:56] WARNING: [pool www] child 5137, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (12.055624 sec), logging [04-Jun-2013 21:24:56] WARNING: [pool www] child 18206, script '/home/site/public_html/index.php' (request: "GET /index.php") execution timed out (15.395149 sec), terminating [04-Jun-2013 21:24:56] WARNING: [pool www] child 17996, script '/home/site/public_html/index.php' (request: "GET /index.php") executing too slow (12.145728 sec), logging [04-Jun-2013 21:24:56] WARNING: [pool www] child 17918, script '/home/site/public_html/index.php' (request: "GET /index.php") execution timed out (18.036700 sec), terminating [04-Jun-2013 21:24:56] NOTICE: child 17996 stopped for tracing [04-Jun-2013 21:24:56] NOTICE: about to trace 17996 [04-Jun-2013 21:24:56] NOTICE: finished trace of 17996 [04-Jun-2013 21:24:56] NOTICE: child 5137 stopped for tracing [04-Jun-2013 21:24:56] NOTICE: about to trace 5137 [04-Jun-2013 21:24:56] NOTICE: finished trace of 5137 [04-Jun-2013 21:24:56] WARNING: [pool www] child 17918 exited on signal 15 (SIGTERM) after 4424.343036 seconds from start [04-Jun-2013 21:24:56] NOTICE: [pool www] child 6706 started [04-Jun-2013 21:24:56] WARNING: [pool www] child 18206 exited on signal 15 (SIGTERM) after 4424.264130 seconds from start [04-Jun-2013 21:24:56] NOTICE: [pool www] child 6707 started [04-Jun-2013 21:24:59] WARNING: [pool www] child 17996, script '/home/site/public_html/index.php' (request: "GET /index.php") execution timed out (15.479201 sec), terminating [04-Jun-2013 21:24:59] WARNING: [pool www] child 17996 exited on signal 15 (SIGTERM) after 4427.655572 seconds from start [04-Jun-2013 21:24:59] NOTICE: [pool www] child 6708 started
这是我的nginxconfiguration
user www-data; worker_processes 4; pid /var/run/nginx.pid; worker_rlimit_nofile 20000; events { #worker_connections 768; #worker_connections 19000; #multi_accept on; use epoll; #worker_connections 10240; worker_connections 4096; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 65; #keepalive_timeout 5; #added client_body_timeout 15; client_header_timeout 15; keepalive_timeout 15; send_timeout 15;
site.conf
proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; #fastcgi_buffers 256 16k; #4096k total fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on;
php5-fpm通过TCP端口连接
谢谢
我想你可能运行了太多的并发php进程,但是如果没有更多关于资源瓶颈的信息,很难知道。 我想象你可能受到磁盘IO和/或CPU的限制,并且所有的并行PHP进程都在争夺这些进程,并相互减慢对方。 在某个时候,进程切换的开销会成为一个重要的因素,并且由于有很多进程在运行,所以吞吐量会减less。 您也可能会陷入RAM风险,并开始交换,这是非常糟糕的情况。 相信nginx能够将请求排队,并保持更高的吞吐量,同时减less请求数量。
我通常会select5到50个PHP进程,这个范围的两端都是有点特殊的。 更一般的是10-15。 对于非常高性能的磁盘系统,比通常的16个左右的内核要多一些,可能需要更多的进程,但与拥有大量更便宜的服务器相比,这通常是一个错误的经济。 根据我的经验,除非你有很多非常糟糕的代码,否则在一台服务器上并行执行超过15个php进程通常没什么好处,如果有一个好处,它可能是稳定的,而不是吞吐量,在面对病态的长时间运行的请求堆积起来,没有备用的进程可用。
如果你有多个代码库和独立的进程池,你可能需要大量的进程,但是你可能不希望每个池有超过3到5个进程。
你需要很多的nginx工作连接来处理静态文件。 在4096之后不太可能有任何改进,只有在特殊的情况下,你会发现1000到4000之间的差异。(除非你主要提供静态文件 – 这是一个完全不同的场景,但是因为你正在谈论关于这个php进程盒我不认为这是这种情况)。
我怀疑你的超时时间太长了。 如果没有任何事情发生,请断开连接并继续下一步。
1)内存 – 我要看的第一件事就是为什么你的脚本需要50MB内存,如果他们所做的只是一个简单的search – 我假设你实际上并没有返回每个用户多个兆字节的数据,如果你每秒提供数百个请求。
MySQL连接器库中存在一个错误,它使PHP为任何TEXT或BLOB分配最大可能的大小,而不仅仅是实际需要的内存量。 这可以通过移动到MySQLND库来解决,不需要更改代码。
2)你的设置pm.max_requests = 10000可能不是一个好的select。 如果每个请求都需要2秒钟,那么您要让进程pipe理器在20,000秒或接近6个小时后重新启动每个进程。 这似乎是一个很长的时间,并且会有足够的时间让内存泄漏来降低进程。 回到500的时候,每15分钟只能重启一次,这对性能没有影响,但可能会更稳定。
3)正如Michael所说,即使你可以允许用户连接多个进程,你仍然需要弄清楚瓶颈在哪里。 即使你一次有好几百个PHP进程,如果他们都只是在等待SQL服务器可用,那么他们总是会排队等待,最终开始超时。
除非你能消除瓶颈,否则你需要实现一个速率限制机制,只允许你的服务器设置可以处理的查询数量,或者优雅地降低拒绝服务器当前无法处理的请求数量。
如果一切都失败了……我想也许你可以在代码中处理这个。 您可以创build一个“票证系统”来同时允许一定数量的search,并为您的用户提供大致的等待时间。 就像“你的search将在N秒内开始”。
我还不能评论(没有足够的代表),所以我会发表一个答案:这将是很好,有你的nginx日志了。
关于你的pool.d / www config:如果你把你的pm设置为静态的,那么你的大部分variables都不会有任何影响,这是max_children,它主要影响你的设置。 ( http://php.net/manual/en/install.fpm.configuration.php )尝试可能与PM“ondemand”。
你不应该从pm.start_servers开始。 你也应该降低你的min_spare_servers。
关于你的nginxconfiguration: http ://wiki.nginx.org/CoreModule#worker_processes“max_clients = worker_processes * worker_connections”
您的值“worker_rlimit_nofile”看起来不正确。
看看http://wiki.nginx.org/CoreModule#worker_cpu_affinity也可以使用你所有的内核。