這幾天web經常出現Nginx 502的問題,先開始也像很多人一樣認為是Nginx的問題,從網上查了查原來是php-fpm在作怪。
web使用的是nginx+php的架構,網站上線還沒多久,所以優化方面基本只是做了些初始的配置。
查看php-fpm.log發現有警告,這些警告和網站的掛了個時間基本吻合。我就從這里開始入手。
先開始也是找了些文檔,但是第二天還是出現問題。后來查看配置文件並翻譯了下(百度),英文底子不好。pm模塊類似apache的模塊,是分靜態和動態的。
網上說的很多調整都是基於動態居多,但是並沒說么定義這個模塊。所以大家用動態和靜態還是要仔細看看配置文件
pm = static
; 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 = 300
; 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 = 50
; 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 = 500
; 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 = 10240
假如使用靜態 pm.max_children這個參數會起作用,其余不會。動態反之。
2G內存pm.max_children大概開啟50左右,按照實際情況來調優,這個是很必要的。