Elton's Blog

Gentoo下Nginx+thin构建rails环境

by on 七.21, 2009, under Linux, Rails

本文前提是你已经配置好了ruby on rails

安装thin

thin是一个ruby的轻量级的web server

可以看到thin在100个并发连接的时候,性能还是不错的。

可以使用

1
sudo gem install thin

或者

1
emerage -av thin

使用emerage的话,需要在/etc/portage/package.keywords中加入

1
2
3
www-servers/thin ~amd64
dev-ruby/eventmachine ~amd64
dev-ruby/rack ~amd64

因为相关的包被gentoo的portage给mask了

创建thin集群rake脚本

进入你的rails应用目录,在lib/tasks下建立一个thin的任务,以.rake为后缀名,如thin.rake。这个是用来建立thin的集群的脚本
编辑内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
namespace :thin do
  namespace :cluster do
 desc 'Start thin cluster'
    task :start => :environment do
      `cd #{RAILS_ROOT}`
      port_range = RAILS_ENV == 'development' ? 3 : 8
      (ENV['SIZE'] ? ENV['SIZE'].to_i : 4).times do |i|
        Thread.new do
          port = ENV['PORT'] ? ENV['PORT'].to_i + i : ("#{port_range}%03d" % i)
          str  = "thin start -d -p#{port} -Ptmp/pids/thin-#{port}.pid"
          str += " -e#{RAILS_ENV}"
          puts str
          puts "Starting server on port #{port}..."
          `#{str}`
        end
      end
    end
desc 'Stop all thin clusters'
    task :stop => :environment do
      `cd #{RAILS_ROOT}`
      Dir.new("#{RAILS_ROOT}/tmp/pids").each do |file|
        Thread.new do
          if file.starts_with?("thin-")
            str  = "thin stop -Ptmp/pids/#{file}"
            puts "Stopping server on port #{file[/d+/]}..."
            `#{str}`
          end
        end
      end
    end
  end
end

之后就可以使用

1
2
# rake thin:cluster:start RAILS_ENV=production SIZE=3 PORT=8000
# rake thin:cluster:stop

来启动和停止thin集群了。

编辑nginx的conf文件,加入rails虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
upstream thin {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
}
 
server {
        listen   80;
        server_name  localhost;
        access_log  /var/log/nginx/localhost.access.log;
        root /var/www/test/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_redirect false;
                if (-f $request_filename/index.html) {
                        rewrite (.*) $1/index.html break;
                }
                if (-f $request_filename.html) {
                        rewrite (.*) $1.html break;
                }
                 if (!-f $request_filename) {
                        proxy_pass http://thin;
                        break;
                }
        }
}

重启nginx就可以运行rails应用了。

参考:
http://code.macournoyer.com/thin/
http://glauche.de/2008/01/12/thin-nginx-with-rails/





:, , ,

2 Comments for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...