Reputation: 29
I have a problem with nginx and php-fpm, i searched already in this form but i found no solution for it.
Server: 16 cores 10gb memory
WARNING: [pool inter] server reached pm.max_children setting (20), consider raising it
[i]
listen = /var/run/fastcgi/i.sock
listen.allowed_clients = 127.0.0.1
listen.group = i
user = i
group = inter
pm = dynamic
pm.max_children = 20
pm.max_requests = 1000
pm.start_servers = 15
pm.min_spare_servers = 15
pm.max_spare_servers = 15
;request_terminate_timeout = 300
php_admin_value[error_log] = /var/log/php-fpm/i-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/ = /
php_admin_value[open_basedir] = /www/public_html/:/tmp:/usr/share/php:/var/lib/php
strong textphp_admin_value[disable_functions] = dl,exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
i tried a lot of differnt settings, but i don't know what is the reason, i hope sombody can help.
Best regards
Upvotes: 2
Views: 10277
Reputation: 19372
The issue happen from misconfiguration fpm pool config.
This confusion happen when You delete comments from default config example.
so I'm fixing Your config example with comments from original example file (www.conf
).
it should be like:
; 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.
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 = 1024
; 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 = 16
; 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 = 64
; 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 = 40
pm.max_requests = 32 ; to avoid memory leaks it's better respawn child after 32 request
personally I don't like idle processes.
I just set:
pm = ondemand
pm.max_children = 1024
pm.process_idle_timeout = 10s
Full example file with comments that explains
what is the meaning concrete param
You can see from here: https://gist.github.com/num8er/43ae78ee6404b12db799946616c36251
Upvotes: 2
Reputation: 2945
The root cause of your problem can't be determined because we don't know what kind of code your server runs.
What happens is that PHP can't process requests fast enough to keep up with nginx.
Raising number of child processes won't help, you have 16 cores and 20 processes. This means that your OS will be forced to schedule processes and raising the number won't make anything faster - you don't even know if you are CPU or I/O bound.
To correctly solve this problem, you need to determine why PHP can't keep up.
You can enable PHP-FPM
slow_log feature by adding this to your pool:
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 1s
This will log every request that took 1 second or longer to file /var/log/php-fpm/slow.log
Inspect the log file and trace back which part of your code causes PHP to respond slowly and fix the real problem
To keep your site responsive until you fix the real problem is that you get another server for PHP processing. Since you are using nginx
and php-fpm
is your backend, then it's quite trivial to set up more servers to handle dynamic request processing. As your site grows and demands grow with it, you can easily add more backend processing power by adding php-fpm
serves to processing pool.
Disclaimer: I am not saying that you should but it's good to know what your options are.
# Define servers in the cluster
upstream phpcluster {
server 10.0.0.1:9000;
server 10.0.0.2:9000;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass phpcluster; # Pass to phpcluster upstream. Additional load balancing rules can be defined in upstream block
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Upvotes: 4