使用Phusion Passenger和Rails时,初始服务器启动缓慢

tsd*_*own 87 deployment ruby-on-rails passenger phusion

为了跳上Phusion Passenger的乐队,我们为一个小型rails应用程序设置了一个临时服务器来测试.

到目前为止,使用它非常好,它使安装/配置和部署应用程序变得轻而易举.问题是我们使用的网站不会经常受到影响,似乎在后台关闭服务器.这意味着当某人进入该站点时,他们会等待很长时间,直到它启动一个新服务器来处理该请求.我们已经阅读了文档,尝试了很多不同的设置(智能/智能-lv2模式,乘客时间等),但仍然没有找到真正的解决方案.

在浏览Google结果后,我们无法真正找到有用的信息.目前,我们有一个cron作业,每隔一段时间发出一次请求,试图让服务器保持运行.

是否有其他人遇到此问题,您对修复有任何建议吗?

Joh*_*hat 118

发生的事情是您的应用程序和/或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
Run Code Online (Sandbox Code Playgroud)

通过使用"智能"生成模式并关闭PassengerMaxPreloaderIdleTime,Passenger将始终在应用程序中保留1个应用程序副本(在启动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
Run Code Online (Sandbox Code Playgroud)

因此,如果您将PassengerPreStart和PassengerMinInstances结合起来,Passenger将在加载apache后立即启动3个实例,并且将始终保持至少3个实例,因此您的用户很少(如果有的话)看到延迟.

或者,如果您已经使用智能产卵(推荐)PassengerMaxPreloaderIdleTime 0,您可以添加PassengerPreStart以获得立即启动的额外好处.

非常感谢phusion.nl的英雄!


Gav*_*Gav 40

只是因为有任何nginx服务器用户绊倒这个问题,'PassengerMaxRequests'和'PassengerStatThrottleRate'指令都没有转换为nginx.然而,其他人做:

rails_spawn_method smart;
rails_app_spawner_idle_time 0;
rails_framework_spawner_idle_time 0;
passenger_pool_idle_time 1000;
Run Code Online (Sandbox Code Playgroud)

HTH!

EDIT rails_spawn_method在乘客3中被弃用而不是使用

passenger_spawn_method smart; 
Run Code Online (Sandbox Code Playgroud)

其他一切都很好,直到约会.

  • 谢谢你.需要注意的一点是,我必须在其主要的nginx.conf中使用其他全局设置填充passenger_pool_idle_time,而不是仅在启用了rails的特定站点配置中填充. (7认同)

Jos*_*osh 5

您还可以使用 PassengerMinInstances:

http://www.modrails.com/documentation/Users%20guide%20Apache.html#PassengerMinInstances

这可以与 PassengerPreStart 结合使用