通过unicorn Web Server加速Rails Server的速度

来源:互联网 发布:淘宝差评在哪里写 编辑:程序博客网 时间:2024/05/16 11:39

在公司使用Rails写的Redmine系统,当连接人员数量较多的时候,系统性能严重恶化。

Google之后了解的到Rails server本身的性能有问题,可以通过使用unicorn web server解决大量并发的问题。

试了一下,安装unicorn还是非常简单的。


1. Rails App的目标下安装 unicorn 模块: gem install unicorn

2. 修改Gemfile,加入unicorn: gem "unicorn"

3. 在configure目录下,创建unicorn.rb 文件

4.unicorn.rb文件中配置unicorn Web server


--------------------------

# config/unicorn.rbworker_processes 3timeout 30preload_app truebefore_fork do |server, worker|  Signal.trap 'TERM' do    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'    Process.kill 'QUIT', Process.pid  end  defined?(ActiveRecord::Base) and    ActiveRecord::Base.connection.disconnect!endafter_fork do |server, worker|  Signal.trap 'TERM' do    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'  end  defined?(ActiveRecord::Base) and    ActiveRecord::Base.establish_connectionend

5. 启动unicorn Server

bundle exec unicorn -p $PORT -c ./config/unicorn.rb


6.rails 运行环境依赖环境变量 RAILS_ENV. 通过设置RAILS_ENV=production 或RAILS_ENV=development等来控制rails的开发环境。



0 0