rails 应用程序的内存优化设置(在 Phusion passenger/nginx 上)
The memory optimal setting of rails app (on Phusion passenger/nginx)
我有一个 rails 应用程序在 Ubuntu 上使用 renv/Phusion Passenger/nginx。
我的服务器内存较小,rails 应用程序很少被调用,所以我需要在 Phusion passenger/nginx 上找到一个内存优化配置。
设置nginx.conf
我尽量减少内存使用,所以我只保留 1 个池,并尽量减少空闲时间。我从这个网站获得信息:https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_pool_idle_time
user www-data;
worker_processes 1;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
...
##
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##
passenger_max_pool_size 1;
passenger_pool_idle_time 1;
passenger_root /home/ubuntu/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.0.21;
passenger_ruby /home/ubuntu/.rbenv/shims/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
设置rails.conf
我有此配置 rails.example.com
已连接到 rails 应用程序。
server {
listen 80;
server_name rails.example.com;
passenger_enabled on;
passenger_app_env development;
root /home/ubuntu/webapp/rails/passenger-ruby-rails-demo/public;
}
用htop
检查内存使用情况,我有这张图。
但是,显示的信息与 sudo passenger-memory-status
命令中的信息并不完全相同。
htop
报告有四个 Passenger RubuApps(具有 3317、3319、3314、3320 pid),但 passenger-memory-status
仅显示 3314 pid。其他进程(core、watchdog、ust-router)也是如此。我认为 htop
显示了正确的内存使用情况。
是什么导致了内存使用信息的差异?我也很好奇
- 如何使用nginx最小化Phusion passenger的内存占用?
- 当没有 rails 应用程序调用时,有没有办法在一定时间后终止乘客进程?
- 除了Phusion passenger,还有什么方法可以使用最少的内存?
编辑
修改 htop
的设置以以树格式显示并以不同颜色显示线程结果显示一个进程。
编辑2
Passenger AppPreloader 和 RubyApp 一样消耗内存。
杀掉AppPreloader后,内存消耗变小了,应用似乎运行良好。
我不确定 AppPreloader 是否绝对必要(为了优化内存使用),是否有不使用它的选项。
我尝试使用 unicorn 服务器启动 rails 应用程序。默认的 WEBRrick 服务器似乎太慢并且仅用于开发目的,所以我跳过了它。
我用 rails new simple
创建了骨架。
设置文件如下
config/unicorn.rb
# set path to application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir
# Set unicorn options
worker_processes 1
preload_app true
timeout 30
# Set up socket location
#listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
listen "/tmp/unicorn.sock", :backlog => 64
# Logging
stderr_path "/var/log/unicorn/stderr.log"
stdout_path "/var/log/unicorn/stdout.log"
# Set master PID location
pid "/tmp/unicorn.pid"
/etc/nginx/site-enabled/rails_unicorn.conf
upstream app {
server unix:/tmp/unicorn.sock fail_timeout=0;
keepalive 8;
}
server {
listen 80;
server_name rails.example.com;
passenger_enabled on;
passenger_app_env development;
root /home/ubuntu/webapp/rails/simple/public;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app/;
proxy_redirect off;
}
}
宝石文件
gem "unicorn-rails"
添加。对于 Rails 4.2.4,gem "rack-handlers"
来自 How can I use unicorn as "rails s"?
的提示
/etc/init/unicorn_rails.conf
# simple uWSGI script
# http://uwsgi-docs.readthedocs.org/en/latest/Upstart.html
description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]
respawn
#
script
chdir /home/ubuntu/webapp/rails/simple
#exec /home/ubuntu/.rbenv/shims/rails server unicorn
exec /home/ubuntu/.rbenv/shims/unicorn -c config/unicorn.rb -D
end script
我的内存占用较小,但有趣的是 ruby 与 Python/uwsgi 相比仍然需要更大的内存。
这里是客运作者。实际上 passenger-memory-stats
显示了最正确的测量值,而不是 htop
。有关详细信息,请参阅 https://www.phusionpassenger.com/library/indepth/accurately_measuring_memory_usage.html。
此外,htop
不仅显示进程,还显示线程。这就是为什么您在 htop 中看到 4 个条目而在 passenger-memory-stats 中看到 1 个条目的原因。因此,passenger-memory-stats 确实是最准确的。
至于您如何在一定时间后关闭应用程序:默认情况下,Passenger 已经这样做了,但您的意思是想稍微调整一下。看看这两个配置选项:
Unicorn 使用的内存并不比 Passenger 少。它们大致相同,因为大部分内存被 Rails 和应用程序占用,所以这并不重要。 Passenger 也比 Unicorn 多features, tooling and documentation。
我有一个 rails 应用程序在 Ubuntu 上使用 renv/Phusion Passenger/nginx。 我的服务器内存较小,rails 应用程序很少被调用,所以我需要在 Phusion passenger/nginx 上找到一个内存优化配置。
设置nginx.conf
我尽量减少内存使用,所以我只保留 1 个池,并尽量减少空闲时间。我从这个网站获得信息:https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_pool_idle_time
user www-data;
worker_processes 1;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
...
##
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##
passenger_max_pool_size 1;
passenger_pool_idle_time 1;
passenger_root /home/ubuntu/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.0.21;
passenger_ruby /home/ubuntu/.rbenv/shims/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
设置rails.conf
我有此配置 rails.example.com
已连接到 rails 应用程序。
server {
listen 80;
server_name rails.example.com;
passenger_enabled on;
passenger_app_env development;
root /home/ubuntu/webapp/rails/passenger-ruby-rails-demo/public;
}
用htop
检查内存使用情况,我有这张图。
但是,显示的信息与 sudo passenger-memory-status
命令中的信息并不完全相同。
htop
报告有四个 Passenger RubuApps(具有 3317、3319、3314、3320 pid),但 passenger-memory-status
仅显示 3314 pid。其他进程(core、watchdog、ust-router)也是如此。我认为 htop
显示了正确的内存使用情况。
是什么导致了内存使用信息的差异?我也很好奇
- 如何使用nginx最小化Phusion passenger的内存占用?
- 当没有 rails 应用程序调用时,有没有办法在一定时间后终止乘客进程?
- 除了Phusion passenger,还有什么方法可以使用最少的内存?
编辑
修改 htop
的设置以以树格式显示并以不同颜色显示线程结果显示一个进程。
编辑2
Passenger AppPreloader 和 RubyApp 一样消耗内存。
杀掉AppPreloader后,内存消耗变小了,应用似乎运行良好。
我不确定 AppPreloader 是否绝对必要(为了优化内存使用),是否有不使用它的选项。
我尝试使用 unicorn 服务器启动 rails 应用程序。默认的 WEBRrick 服务器似乎太慢并且仅用于开发目的,所以我跳过了它。
我用 rails new simple
创建了骨架。
设置文件如下
config/unicorn.rb
# set path to application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir
# Set unicorn options
worker_processes 1
preload_app true
timeout 30
# Set up socket location
#listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
listen "/tmp/unicorn.sock", :backlog => 64
# Logging
stderr_path "/var/log/unicorn/stderr.log"
stdout_path "/var/log/unicorn/stdout.log"
# Set master PID location
pid "/tmp/unicorn.pid"
/etc/nginx/site-enabled/rails_unicorn.conf
upstream app {
server unix:/tmp/unicorn.sock fail_timeout=0;
keepalive 8;
}
server {
listen 80;
server_name rails.example.com;
passenger_enabled on;
passenger_app_env development;
root /home/ubuntu/webapp/rails/simple/public;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app/;
proxy_redirect off;
}
}
宝石文件
gem "unicorn-rails"
添加。对于 Rails 4.2.4,gem "rack-handlers"
来自 How can I use unicorn as "rails s"?
的提示/etc/init/unicorn_rails.conf
# simple uWSGI script
# http://uwsgi-docs.readthedocs.org/en/latest/Upstart.html
description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]
respawn
#
script
chdir /home/ubuntu/webapp/rails/simple
#exec /home/ubuntu/.rbenv/shims/rails server unicorn
exec /home/ubuntu/.rbenv/shims/unicorn -c config/unicorn.rb -D
end script
我的内存占用较小,但有趣的是 ruby 与 Python/uwsgi 相比仍然需要更大的内存。
这里是客运作者。实际上 passenger-memory-stats
显示了最正确的测量值,而不是 htop
。有关详细信息,请参阅 https://www.phusionpassenger.com/library/indepth/accurately_measuring_memory_usage.html。
此外,htop
不仅显示进程,还显示线程。这就是为什么您在 htop 中看到 4 个条目而在 passenger-memory-stats 中看到 1 个条目的原因。因此,passenger-memory-stats 确实是最准确的。
至于您如何在一定时间后关闭应用程序:默认情况下,Passenger 已经这样做了,但您的意思是想稍微调整一下。看看这两个配置选项:
Unicorn 使用的内存并不比 Passenger 少。它们大致相同,因为大部分内存被 Rails 和应用程序占用,所以这并不重要。 Passenger 也比 Unicorn 多features, tooling and documentation。