[英]The memory optimal setting of rails app (on Phusion passenger/nginx)
I have a rails app using renv/Phusion Passenger/nginx on Ubuntu. 我在Ubuntu上有一个使用renv / Phusion Passenger / nginx的Rails应用程序。 My server has a small memory size, and the rails app is rarely invoked, so I need to find a memory optimal configuration on Phusion passenger/nginx.
我的服务器内存较小,很少调用rails应用程序,因此我需要在Phusion passenger / nginx上找到内存最佳配置。
I tried to minimize the memory usage, so I keep only 1 pool, and tried to minimize the idle time. 我试图最小化内存使用量,所以我只保留了1个池,并试图最小化空闲时间。 I got information from this site: https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_pool_idle_time
我从以下站点获得了信息: 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/*;
}
I have this configuration to rails.example.com
is connected to a rails app. 我已经将此配置连接到
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;
}
Checking the memory usage with htop
, I have this map. 用
htop
检查内存使用情况,我有这张地图。
However, the information shown is not exactly the same as the one from sudo passenger-memory-status
command. 但是,显示的信息与
sudo passenger-memory-status
命令中的信息并不完全相同。
The htop
report has four Passenger RubuApps (with 3317, 3319, 3314, 3320 pids), but passenger-memory-status
only shows 3314 pid. htop
报告具有四个Passenger RubuApps(分别具有3317、3319、3314和3320 PID),但是passenger-memory-status
仅显示3314 PID。 The same is true with other processes (core, watchdog, ust-router). 其他进程(核心,看门狗,ust-router)也是如此。 I think
htop
shows the correct memory usage. 我认为
htop
显示正确的内存使用情况。
What makes the differences in memory usage information? 是什么使内存使用情况信息有所不同? I'm also curious about
我也很好奇
Modifying the setup of htop
to show in tree format and show threads in different color results in showing one process. 修改
htop
的设置以树状显示并以不同颜色显示线程会导致显示一个进程。
Passenger AppPreloader consumes the memory as much as RubyApp does. 乘客AppPreloader与RubyApp一样消耗内存。
After killing the AppPreloader, the memory consumption is smaller, and the app seems to be working fine. 杀死AppPreloader之后,内存消耗较小,并且该应用程序似乎运行正常。
I'm not sure if AppPreloader is absolutely necessary (for optimal memory usage) and is there an option not to use it. 我不确定AppPreloader是否绝对必要(为了最佳使用内存),是否可以选择不使用它。
Passenger author here. 乘客作者在这里。 It is in fact
passenger-memory-stats
that shows the most correct measurement, not htop
. 事实上,这是
passenger-memory-stats
显示最正确的测量,而不是htop
。 See https://www.phusionpassenger.com/library/indepth/accurately_measuring_memory_usage.html for more information. 有关更多信息,请参见https://www.phusionpassenger.com/library/indepth/accurately_measuring_memory_usage.html 。
Furthermore, htop
not only displays processes, but also threads . 此外,
htop
不仅显示进程,而且显示线程 。 That is the reason why you see 4 entries in htop and 1 in passenger-memory-stats is because. 这就是为什么您在htop中看到4个条目,在guest-memory-stats中看到1个条目的原因,是因为。 Thus, passenger-memory-stats really is the most accurate.
因此,乘客记忆状态确实是最准确的。
As for your how to shutdown an app after a certain amount of time: Passenger already does that by default, but you mean want to tweak things a little. 至于您在一定时间后如何关闭应用程序:默认情况下,Passenger已经这样做,但是您的意思是要稍微调整一下。 Have a look at these two config options:
看一下这两个配置选项:
Unicorn does not use less memory than Passenger. 独角兽使用的内存不会少于旅客。 They are about the same because most memory is occupied by Rails and by the app, so it does not really matter.
它们大致相同,因为大多数内存都由Rails和应用程序占用,因此并不重要。 Passenger has a ton more features, tooling and documentation than Unicorn too.
乘客比Unicorn拥有更多的功能,工具和文档 。
I tried with unicorn server to launch rails app. 我尝试与独角兽服务器启动Rails应用程序。 The default WEBRrick server seems to be too slow and only for development purposes, so I skipped it.
默认的WEBRrick服务器似乎太慢,仅用于开发目的,因此我跳过了它。
I used rails new simple
to create a skeleton. 我使用
rails new simple
创建了一个骨架。 The setting files are as follows. 设置文件如下。
# 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"
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"
added. gem "unicorn-rails"
。 For Rails 4.2.4, gem "rack-handlers" 对于Rails 4.2.4,宝石为“机架处理程序”
Hint from How can I use unicorn as "rails s"? 提示: 如何将独角兽用作“ rails s”?
# 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
# https://stackoverflow.com/questions/14823972/upstart-node-js-working-directory
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
I have smaller memory footprint, but it's interesting to see that ruby still requires larger memory size, compared to the Python/uwsgi. 我的内存占用空间较小,但有趣的是,与Python / uwsgi相比,ruby仍需要更大的内存大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.