繁体   English   中英

Rails应用程序的内存最佳设置(在Phusion乘客/ nginx上)

[英]The memory optimal setting of rails app (on Phusion passenger/nginx)

我在Ubuntu上有一个使用renv / Phusion Passenger / nginx的Rails应用程序。 我的服务器内存较小,很少调用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。 其他进程(核心,看门狗,ust-router)也是如此。 我认为htop显示正确的内存使用情况。

是什么使内存使用情况信息有所不同? 我也很好奇

  1. 如何使用Nginx最小化Phusion乘客的内存使用量?
  2. 在没有Rails应用程序调用的情况下,是否可以在一定时间后终止乘客流程?
  3. 除了Phusion乘客以外,使用最少内存量的方法可能是什么?

编辑

修改htop的设置以树状显示并以不同颜色显示线程会导致显示一个进程。

在此处输入图片说明

编辑2

乘客AppPreloader与RubyApp一样消耗内存。 在此处输入图片说明

杀死AppPreloader之后,内存消耗较小,并且该应用程序似乎运行正常。

在此处输入图片说明

我不确定AppPreloader是否绝对必要(为了最佳使用内存),是否可以选择不使用它。

乘客作者在这里。 事实上,这是passenger-memory-stats显示最正确的测量,而不是htop 有关更多信息,请参见https://www.phusionpassenger.com/library/indepth/accurately_measuring_memory_usage.html

此外, htop不仅显示进程,而且显示线程 这就是为什么您在htop中看到4个条目,在guest-memory-stats中看到1个条目的原因,是因为。 因此,乘客记忆状态确实是最准确的。

至于您在一定时间后如何关闭应用程序:默认情况下,Passenger已经这样做,但是您的意思是要稍微调整一下。 看一下这两个配置选项:

独角兽使用的内存不会少于旅客。 它们大致相同,因为大多数内存都由Rails和应用程序占用,因此并不重要。 乘客比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,宝石为“机架处理程序”

提示: 如何将独角兽用作“ 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

# 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

我的内存占用空间较小,但有趣的是,与Python / uwsgi相比,ruby仍需要更大的内存大小。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM