PS:第一次写的很详细,可惜发布失败,然后全没了,这是第二次,表示只贴代码,剩下的自己领悟好了,这就是所谓的一鼓作气再而衰吧,希望没有第三次。
版本:
ruby 2.1.0
rails 4.0.2
nginx 1.5.8 mainline version
unicorn 4.7.0
/usr/local/nginx/conf/nginx.conf
1 user NginxUser NginxGroup; 2 worker_processes 1; 3 4 events { 5 worker_connections 1024; 6 } 7 8 http { 9 include mime.types; 10 include /usr/local/nginx/conf/conf.d/*.conf; 11 default_type application/octet-stream; 12 13 sendfile on; 14 #tcp_nopush on; 15 16 keepalive_timeout 65; 17 18 gzip on; 19 }
/usr/local/nginx/conf/conf.d/guorj.conf
1 upstream guorj_rails_unicorn_server { 2 server unix:/tmp/guorj_unicorn.sock fail_timeout=0; 3 #server 127.0.0.1:8081 fail_timeout=0; 4 } 5 server { 6 listen 80; 7 client_max_body_size 4G; 8 server_name guorj.com; 9 keepalive_timeout 5; 10 root /home/NginxUser/www/guorj/public; 11 access_log /home/NginxUser/www/guorj/log/nginx_access.log; 12 error_log /home/NginxUser/www/guorj/log/nginx_error.log; 13 rewrite_log on; 14 index index.html; 15 16 location / { 17 proxy_set_header Host $host; 18 proxy_set_header X-Forwarded-Host $host; 19 proxy_set_header X-Real-IP $remote_addr; 20 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 21 proxy_buffering on; 22 proxy_redirect off; 23 proxy_pass http://guorj_rails_unicorn_server; 24 } 25 #Rails error pages 26 location =/500.html { 27 root /home/NginxUser/www/guorj/public; 28 } 29 }
/home/NginxUser/www/guorj/config/unicorn.rb
1 # Sample verbose configuration file for Unicorn (not Rack) 2 # 3 # This configuration file documents many features of Unicorn 4 # that may not be needed for some applications. See 5 # http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb 6 # for a much simpler configuration file. 7 # 8 # See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete 9 # documentation. 10 # can't specify rails env 11 rails_env = ENV["RAILS_ENV"] || "production" 12 13 # Use at least one worker per core if you're on a dedicated server, 14 # more will usually help for _short_ waits on databases/caches. 15 worker_processes 4 16 17 # Since Unicorn is never exposed to outside clients, it does not need to 18 # run on the standard HTTP port (80), there is no reason to start Unicorn 19 # as root unless it's from system init scripts. 20 # If running the master process as root and the workers as an unprivileged 21 # user, do this to switch euid/egid in the workers (also chowns logs): 22 # user "unprivileged_user", "unprivileged_group" 23 24 # Help ensure your application will always spawn in the symlinked 25 # "current" directory that Capistrano sets up. 26 27 app_root = File.expand_path('.') 28 29 working_directory app_root #"/path/to/app/current" # available in 0.94.0+ 30 31 # listen on both a Unix domain socket and a TCP port, 32 # we use a shorter backlog for quicker failover when busy 33 #listen "/path/to/.unicorn.sock", :backlog => 64 34 #listen "#{app_root}/tmp/sockets/unicorn.sock", :backlog => 64 35 listen "/tmp/guorj_unicorn.sock", :backlog => 64 36 listen 8081, :tcp_nopush => false # true 37 38 # nuke workers after 30 seconds instead of 60 seconds (the default) 39 timeout 120 # 30 40 41 # feel free to point this anywhere accessible on the filesystem 42 #pid "/path/to/app/shared/pids/unicorn.pid" 43 #pid "#{app_root}/tmp/pids/unicorn.pid" 44 pid "/tmp/guorj_unicorn.pid" 45 46 # By default, the Unicorn logger will write to stderr. 47 # Additionally, ome applications/frameworks log to stderr or stdout, 48 # so prevent them from going to /dev/null when daemonized here: 49 #stderr_path "/path/to/app/shared/log/unicorn.stderr.log" 50 #stdout_path "/path/to/app/shared/log/unicorn.stdout.log" 51 stderr_path "#{app_root}/log/unicorn.stderr.log" 52 stdout_path "#{app_root}/log/unicorn.stdout.log" 53 54 # combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings 55 # http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow 56 preload_app true 57 GC.respond_to?(:copy_on_write_friendly=) and 58 GC.copy_on_write_friendly = true 59 60 # Enable this flag to have unicorn test client connections by writing the 61 # beginning of the HTTP headers before calling the application. This 62 # prevents calling the application for connections that have disconnected 63 # while queued. This is only guaranteed to detect clients on the same 64 # host unicorn runs on, and unlikely to detect disconnects even on a 65 # fast LAN. 66 # check_client_connection false 67 68 before_fork do |server, worker| 69 # the following is highly recomended for Rails + "preload_app true" 70 # as there's no need for the master process to hold a connection 71 defined?(ActiveRecord::Base) and 72 ActiveRecord::Base.connection.disconnect! 73 74 # The following is only recommended for memory/DB-constrained 75 # installations. It is not needed if your system can house 76 # twice as many worker_processes as you have configured. 77 # 78 # # This allows a new master process to incrementally 79 # # phase out the old master process with SIGTTOU to avoid a 80 # # thundering herd (especially in the "preload_app false" case) 81 # # when doing a transparent upgrade. The last worker spawned 82 # # will then kill off the old master process with a SIGQUIT. 83 old_pid = "#{server.config[:pid]}.oldbin" 84 if old_pid != server.pid 85 begin 86 sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU 87 Process.kill(sig, File.read(old_pid).to_i) 88 rescue Errno::ENOENT, Errno::ESRCH 89 end 90 end 91 # 92 # Throttle the master from forking too quickly by sleeping. Due 93 # to the implementation of standard Unix signal handlers, this 94 # helps (but does not completely) prevent identical, repeated signals 95 # from being lost when the receiving process is busy. 96 sleep 1 97 end 98 99 after_fork do |server, worker| 100 # per-process listener ports for debugging/admin/migrations 101 # addr = "127.0.0.1:#{9293 + worker.nr}" 102 # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true) 103 104 # the following is *required* for Rails + "preload_app true", 105 defined?(ActiveRecord::Base) and 106 ActiveRecord::Base.establish_connection 107 108 # if preload_app is true, then you may also want to check and 109 # restart any other shared sockets/descriptors such as Memcached, 110 # and Redis. TokyoCabinet file handles are safe to reuse 111 # between any number of forked children (assuming your kernel 112 # correctly implements pread()/pwrite() system calls) 113 end
然后运行unicorn
unicorn_rails -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D
添加第二个rails项目(请自行和第一个项目的配置对比不同,被网速把激情磨灭了)
/usr/local/nginx/conf/conf.d/wguorj.conf
1 upstream wguorj_rails_unicorn_server { 2 server unix:/tmp/wguorj_unicorn.sock fail_timeout=0; 3 #server 127.0.0.1:8082 fail_timeout=0; 4 } 5 server { 6 listen 80; 7 client_max_body_size 4G; 8 server_name www.guorj.com; 9 keepalive_timeout 5; 10 root /home/NginxUser/www/wguorj/public; 11 access_log /home/NginxUser/www/wguorj/log/nginx_access.log; 12 error_log /home/NginxUser/www/wguorj/log/nginx_error.log; 13 rewrite_log on; 14 index index.html; 15 16 location / { 17 proxy_set_header Host $host; 18 proxy_set_header X-Forwarded-Host $host; 19 proxy_set_header X-Real-IP $remote_addr; 20 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 21 proxy_buffering on; 22 proxy_redirect off; 23 proxy_pass http://wguorj_rails_unicorn_server; 24 } 25 #Rails error pages 26 location =/500.html { 27 root /home/NginxUser/www/wguorj/public; 28 } 29 }
/home/NginxUser/www/wguorj/config/unicorn.rb
1 # Sample verbose configuration file for Unicorn (not Rack) 2 # 3 # This configuration file documents many features of Unicorn 4 # that may not be needed for some applications. See 5 # http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb 6 # for a much simpler configuration file. 7 # 8 # See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete 9 # documentation. 10 # can't specify rails env 11 rails_env = ENV["RAILS_ENV"] || "production" 12 13 # Use at least one worker per core if you're on a dedicated server, 14 # more will usually help for _short_ waits on databases/caches. 15 worker_processes 4 16 17 # Since Unicorn is never exposed to outside clients, it does not need to 18 # run on the standard HTTP port (80), there is no reason to start Unicorn 19 # as root unless it's from system init scripts. 20 # If running the master process as root and the workers as an unprivileged 21 # user, do this to switch euid/egid in the workers (also chowns logs): 22 # user "unprivileged_user", "unprivileged_group" 23 24 # Help ensure your application will always spawn in the symlinked 25 # "current" directory that Capistrano sets up. 26 27 app_root = File.expand_path('.') 28 29 working_directory app_root #"/path/to/app/current" # available in 0.94.0+ 30 31 # listen on both a Unix domain socket and a TCP port, 32 # we use a shorter backlog for quicker failover when busy 33 #listen "/path/to/.unicorn.sock", :backlog => 64 34 #listen "#{app_root}/tmp/sockets/unicorn.sock", :backlog => 64 35 listen "/tmp/wguorj_unicorn.sock", :backlog => 64 36 listen 8082, :tcp_nopush => false # true 37 38 # nuke workers after 30 seconds instead of 60 seconds (the default) 39 timeout 120 # 30 40 41 # feel free to point this anywhere accessible on the filesystem 42 #pid "/path/to/app/shared/pids/unicorn.pid" 43 #pid "#{app_root}/tmp/pids/unicorn.pid" 44 pid "/tmp/wguorj_unicorn.pid" 45 46 # By default, the Unicorn logger will write to stderr. 47 # Additionally, ome applications/frameworks log to stderr or stdout, 48 # so prevent them from going to /dev/null when daemonized here: 49 #stderr_path "/path/to/app/shared/log/unicorn.stderr.log" 50 #stdout_path "/path/to/app/shared/log/unicorn.stdout.log" 51 stderr_path "#{app_root}/log/unicorn.stderr.log" 52 stdout_path "#{app_root}/log/unicorn.stdout.log" 53 54 # combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings 55 # http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow 56 preload_app true 57 GC.respond_to?(:copy_on_write_friendly=) and 58 GC.copy_on_write_friendly = true 59 60 # Enable this flag to have unicorn test client connections by writing the 61 # beginning of the HTTP headers before calling the application. This 62 # prevents calling the application for connections that have disconnected 63 # while queued. This is only guaranteed to detect clients on the same 64 # host unicorn runs on, and unlikely to detect disconnects even on a 65 # fast LAN. 66 # check_client_connection false 67 68 before_fork do |server, worker| 69 # the following is highly recomended for Rails + "preload_app true" 70 # as there's no need for the master process to hold a connection 71 defined?(ActiveRecord::Base) and 72 ActiveRecord::Base.connection.disconnect! 73 74 # The following is only recommended for memory/DB-constrained 75 # installations. It is not needed if your system can house 76 # twice as many worker_processes as you have configured. 77 # 78 # # This allows a new master process to incrementally 79 # # phase out the old master process with SIGTTOU to avoid a 80 # # thundering herd (especially in the "preload_app false" case) 81 # # when doing a transparent upgrade. The last worker spawned 82 # # will then kill off the old master process with a SIGQUIT. 83 old_pid = "#{server.config[:pid]}.oldbin" 84 if old_pid != server.pid 85 begin 86 sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU 87 Process.kill(sig, File.read(old_pid).to_i) 88 rescue Errno::ENOENT, Errno::ESRCH 89 end 90 end 91 # 92 # Throttle the master from forking too quickly by sleeping. Due 93 # to the implementation of standard Unix signal handlers, this 94 # helps (but does not completely) prevent identical, repeated signals 95 # from being lost when the receiving process is busy. 96 sleep 1 97 end 98 99 after_fork do |server, worker| 100 # per-process listener ports for debugging/admin/migrations 101 # addr = "127.0.0.1:#{9293 + worker.nr}" 102 # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true) 103 104 # the following is *required* for Rails + "preload_app true", 105 defined?(ActiveRecord::Base) and 106 ActiveRecord::Base.establish_connection 107 108 # if preload_app is true, then you may also want to check and 109 # restart any other shared sockets/descriptors such as Memcached, 110 # and Redis. TokyoCabinet file handles are safe to reuse 111 # between any number of forked children (assuming your kernel 112 # correctly implements pread()/pwrite() system calls) 113 end
运行第二个rails程序 记得重启nginx
unicorn_rails -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D
相关参考资料
求 Nginx + Unicorn 部署 Rails 完整配置 http://ruby-china.org/topics/471
https://github.com/huacnlee/ruby-china/blob/master/config/unicorn.rb
https://raw.github.com/defunkt/unicorn/master/examples/unicorn.conf.rb
unicorn官网:http://unicorn.bogomips.org/
注运行两个rails项目的进程
[root@GrjServer guorj]# ps aux | grep nginx root 1180 0.0 0.2 45108 1256 ? Ss 17:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx 500 1181 0.0 0.3 45536 1820 ? S 17:34 0:00 nginx: worker process root 1184 0.0 0.1 103244 856 pts/0 S+ 17:34 0:00 grep nginx [root@GrjServer guorj]# ps aux | grep unicorn root 1116 0.0 12.5 272756 62856 ? Sl 17:00 0:01 unicorn_rails master -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D root 1119 0.0 12.3 272756 61680 ? Sl 17:00 0:00 unicorn_rails worker[0] -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D root 1122 0.0 12.3 272756 61680 ? Sl 17:00 0:00 unicorn_rails worker[1] -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D root 1125 0.0 12.3 272756 61680 ? Sl 17:00 0:00 unicorn_rails worker[2] -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D root 1128 0.0 12.3 272756 61680 ? Sl 17:00 0:00 unicorn_rails worker[3] -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D root 1159 1.2 12.5 272912 63008 ? Sl 17:33 0:01 unicorn_rails master -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D root 1162 0.0 12.3 272912 61820 ? Sl 17:33 0:00 unicorn_rails worker[0] -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D root 1165 0.0 12.3 272912 61820 ? Sl 17:33 0:00 unicorn_rails worker[1] -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D root 1168 0.0 12.3 272912 61820 ? Sl 17:33 0:00 unicorn_rails worker[2] -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D root 1171 0.0 12.3 272912 61820 ? Sl 17:33 0:00 unicorn_rails worker[3] -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D root 1186 0.0 0.1 103244 856 pts/0 S+ 17:34 0:00 grep unicorn [root@GrjServer guorj]#