发生的事情是您的 Application 和/或 ApplicationSpawners 由于超时而关闭。要处理您的新请求,Passenger 必须启动您的应用程序的新副本,这可能需要几秒钟,即使在快速机器上也是如此。要解决此问题,您可以使用一些 Apache 配置选项来保持应用程序处于活动状态。
以下是我在服务器上所做的具体操作。PassengerSpawnMethod 和PassengerMaxPreloaderIdleTime 是在您的情况下最重要的配置选项。
# Speeds up spawn time tremendously -- if your app is compatible.
# RMagick seems to be incompatible with smart spawning
# Older versions of Passenger called this RailsSpawnMethod
PassengerSpawnMethod smart
# Keep the application instances alive longer. Default is 300 (seconds)
PassengerPoolIdleTime 1000
# Keep the spawners alive, which speeds up spawning a new Application
# listener after a period of inactivity at the expense of memory.
# Older versions of Passenger called this RailsAppSpawnerIdleTime
PassengerMaxPreloaderIdleTime 0
# Just in case you're leaking memory, restart a listener
# after processing 5000 requests
PassengerMaxRequests 5000
通过使用“智能”生成模式并关闭PassengerMaxPreloaderIdleTime,Passenger 将始终在内存中保留一份应用程序副本(在启动Apache 后的第一个请求之后)。将从该副本中编辑单个Application
侦听fork
器,这是一种超级便宜的操作。它发生得如此之快,以至于您无法判断您的应用程序是否必须生成侦听器。
如果您的应用程序与智能生成不兼容,我建议您保持较大的 PassengerPoolIdleTime 并定期使用 curl 和 cronjob 或 monit 或其他方式访问您的站点,以确保侦听器保持活跃。
乘客用户指南是这些和更多配置选项的绝佳参考。
编辑:如果您的应用与智能生成不兼容,有一些非常好的新选项
# Automatically hit your site when apache starts, so that you don't have to wait
# for the first request for passenger to "spin up" your application. This even
# helps when you have smart spawning enabled.
PassengerPreStart http://myexample.com/
PassengerPreStart http://myexample2.com:3500/
# the minimum number of application instances that must be kept around whenever
# the application is first accessed or after passenger cleans up idle instances
# With this option, 3 application instances will ALWAYS be available after the
# first request, even after passenger cleans up idle ones
PassengerMinInstances 3
因此,如果您结合PassengerPreStart 和PassengerMinInstances,Passenger 将在apache 加载后立即启动3 个实例,并且始终保持至少3 个实例启动,因此您的用户很少(如果有的话)会看到延迟。
或者,如果您已经使用智能生成(推荐)PassengerMaxPreloaderIdleTime 0
,您可以添加PassengerPreStart
以获得立即启动的额外好处。
非常感谢phusion.nl的英雄们!