Nginx 502 Bad Gateway的含义是请求的PHP-CGI已经执行,但是由于某种原因(一般是读取资源的问题)没有执行完毕而导致PHP-CGI进程终止。
Nginx 504 Gateway Time-out的含义是所请求的网关没有请求到,简单来说就是没有请求到可以执行的PHP-CGI。
出现错误最重要的事查看错误信息;一般错误信息提示能够直接有效的让你找到解决办法。
查看错误日志显示:
[28-Sep-2017 17:31:42] WARNING: [pool www] server reached pm.max_children setting (8), consider raising it
[28-Sep-2017 20:18:27] NOTICE: Terminating ...
[28-Sep-2017 20:18:27] NOTICE: exiting, bye-bye!
[28-Sep-2017 20:18:56] NOTICE: fpm is running, pid 1253
[28-Sep-2017 20:18:56] NOTICE: ready to handle connections
[28-Sep-2017 20:46:05] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 4 total children
[28-Sep-2017 23:52:10] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 4 total children
[28-Sep-2017 23:52:11] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 0 idle, and 5 total children
[28-Sep-2017 23:52:12] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 6 total children
[28-Sep-2017 23:52:39] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 4 total children
server reached pm.max_children setting (8), consider raising it
提示我们服务器子进程已达最大值 pm.max_children 需要增大该值;
seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 6 total children
错误提示需要增大 pm.start_servers 或 pm.min_spare_servers 或 pm.max_spare_servers
; Choose how the process manager will control the number of child processes.
; Possible Values:
; static - a fixed number (pm.max_children) of child processes;
; dynamic - the number of child processes are set dynamically based on the
; following directives. With this process management, there will be
; always at least 1 children.
; pm.max_children - the maximum number of children that can
; be alive at the same time.
; pm.start_servers - the number of children created on startup.
; pm.min_spare_servers - the minimum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is less than this
; number then some children will be created.
; pm.max_spare_servers - the maximum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is greater than this
; number then some children will be killed.
; ondemand - no children are created at startup. Children will be forked when
; new requests will connect. The following parameter are used:
; pm.max_children - the maximum number of children that
; can be alive at the same time.
; pm.process_idle_timeout - The number of seconds after which
; an idle process will be killed.
; Note: This value is mandatory.
; 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 //10 + (50 - 10)/2
经验人士的总结:
一般php-fpm进程占用20~30m左右的内存就按30m算。如果单独跑php-fpm,动态方式起始值可设置物理内存Mem/30M,由于大家一般Nginx、MySQL都在一台机器上,于是预留一半给它们,即php-fpm进程数为$Mem/2/30(单位相同)。
1核2G的服务器配置如下:
pm = dynamic
pm.max_children = 50
pm.start_servers = 30
pm.min_spare_servers = 10
pm.max_spare_servers = 50