[英]The memory optimal setting of rails app (on Phusion passenger/nginx)
我在Ubuntu上有一個使用renv / Phusion Passenger / nginx的Rails應用程序。 我的服務器內存較小,很少調用rails應用程序,因此我需要在Phusion passenger / nginx上找到內存最佳配置。
我試圖最小化內存使用量,所以我只保留了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.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
顯示正確的內存使用情況。
是什么使內存使用情況信息有所不同? 我也很好奇
修改htop
的設置以樹狀顯示並以不同顏色顯示線程會導致顯示一個進程。
殺死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
創建了一個骨架。 設置文件如下。
# 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"
。 對於Rails 4.2.4,寶石為“機架處理程序”
# 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.