我们将Nginx放到前端负责转发请求到后端一系列的thin进程去处理
首先将Thin安装上去:
[root@li96-10 ~]# gem install thin
Building native extensions. This could take a while...
Successfully installed thin-1.2.5
1 gem installed
Installing ri documentation for thin-1.2.5...
Installing RDoc documentation for thin-1.2.5...
thin同时还以来rack eventmachine,没有的话安装之
我们准备写一个rake任务来启动和关闭thin 服务
在rails的项目目录/lib/tasks下创建一个thin.rake文件:( 来源 ) Ruby脚本很不错哦
namespace :thin do
desc 'Start thin cluster'
task :start => :environment do
`cd #{RAILS_ROOT}`
port_range = RAILS_ENV == 'developmet' ? 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
启动和关闭thin服务的命令:
rake thin:start RAILS_ENV=production SIZE=3 PORT=8000
rake thin:stop RAILS_ENV=production
修改Ngin的配置文件/usr/local/system/nginx/conf/nginx.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# thin集群
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 www.weekface.info;
access_log logs/blog.access.log;
error_log logs/blog.error.log info;
#网站跟目录
root /usr/local/system/www/blog/current/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 off;
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; #ngnix负责前端转发到后端的thin集群
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
最后还要修改deploy.rb文件:
task :start do
run "cd #{release_path} && /usr/local/system/ruby-1.8.7-p72/bin/rake thin:start RAILS_ENV=production SIZE=3 PORT=8000"
end
task :stop do
run "cd #{release_path} && /usr/local/system/ruby-1.8.7-p72/bin/rake thin:stop"
end
task :restart do
deploy.stop
deploy.start
end
转载于:https://blog.51cto.com/writerok/710826