在使用Nginx和PHP-FPM的pm.max_children之后,请求永远不会排队

前端之家收集整理的这篇文章主要介绍了在使用Nginx和PHP-FPM的pm.max_children之后,请求永远不会排队前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一旦池到达pm.max_children,Nginx就会在尝试向PHP-FPM发送新请求时开始超时. PHP-status页面中的“max listen queue”始终为0.

> PHP-FPM 5.5.16
> Nginx 1.6.1

以下是PHP-fpm池的示例:

  1. [example]
  2. catch_workers_output = no
  3. ; Configure listener
  4. listen = /var/run/PHP-fpm/example.sock
  5. listen.backlog = 65535
  6. listen.owner = Nginx
  7. listen.group = Nginx
  8. ; Unix user/group of processes
  9. user = Nginx
  10. group = Nginx
  11. ; Choose how the process manager will control the number of child processes.
  12. pm = ondemand
  13. pm.max_children = 10
  14. pm.max_requests = 200
  15. pm.process_idle_timeout = 30s
  16. pm.status_path = /status
  17. ; Pass environment variables
  18. env[HOSTNAME] = $HOSTNAME
  19. env[PATH] = /usr/local/bin:/usr/bin:/bin
  20. env[TMP] = /tmp
  21. env[TMPDIR] = /tmp
  22. env[TEMP] = /tmp
  23. ; Host specific PHP ini settings here
  24. PHP_admin_flag[log_errors] = on
  25. PHP_admin_value[open_basedir] = /tmp:/var/www/apc:/var/www/wordpress/example
  26. PHP_admin_value[error_log] = /var/log/PHP-fpm/example.log
最佳答案
由于这个问题仍然存在于未解答的问题中,我将尝试一个过时的答案.根据PHP manual,显示错误是设置“pm.max_children”的预期行为:

The number of child processes … to be created when pm is set to dynamic.

This option sets the limit on the number of simultaneous requests that will be served.

但是,每个请求都应该很快处理,因此下一个请求的过程是免费的.如果没有,Nginx可能会在无法处理更多请求时立即报告“502 Bad Gateway”.

仔细检查listen.backlog的PHP-fpm配置中设置的值.这定义了队列长度(reference):

The backlog argument defines the maximum length to which the queue of pending connections

但是,此值受底层系统的限制.看到:

  1. sysctl net.core.somaxconn

据我所知,没有办法将请求排队到上游(PHP-fpm),如果这会引发错误.但是,如果发生错误,您可以告诉Nginx切换到另一个进程.例如,这可能会触发客户端重新加载.

如果它不是listen-backlog / net.core.somaxconn设置,那么实际问题就是请求阻止PHP-fpm进程这么久的原因.

猜你在找的Nginx相关文章