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

 user NginxUser NginxGroup;
worker_processes ; events {
worker_connections ;
} http {
include mime.types;
include /usr/local/nginx/conf/conf.d/*.conf;
default_type application/octet-stream; sendfile on;
#tcp_nopush on; keepalive_timeout 65; gzip on;
}

/usr/local/nginx/conf/conf.d/guorj.conf

 upstream guorj_rails_unicorn_server {
server unix:/tmp/guorj_unicorn.sock fail_timeout=;
#server 127.0.0.1: fail_timeout=;
}
server {
listen ;
client_max_body_size 4G;
server_name guorj.com;
keepalive_timeout ;
root /home/NginxUser/www/guorj/public;
access_log /home/NginxUser/www/guorj/log/nginx_access.log;
error_log /home/NginxUser/www/guorj/log/nginx_error.log;
rewrite_log on;
index index.html; location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_redirect off;
proxy_pass http://guorj_rails_unicorn_server;
}
#Rails error pages
location =/.html {
root /home/NginxUser/www/guorj/public;
}
}

/home/NginxUser/www/guorj/config/unicorn.rb

 # Sample verbose configuration file for Unicorn (not Rack)
#
# This configuration file documents many features of Unicorn
# that may not be needed for some applications. See
# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
# for a much simpler configuration file.
#
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.
# can't specify rails env
rails_env = ENV["RAILS_ENV"] || "production" # Use at least one worker per core if you're on a dedicated server,
# more will usually help for _short_ waits on databases/caches.
worker_processes 4 # Since Unicorn is never exposed to outside clients, it does not need to
# run on the standard HTTP port (80), there is no reason to start Unicorn
# as root unless it's from system init scripts.
# If running the master process as root and the workers as an unprivileged
# user, do this to switch euid/egid in the workers (also chowns logs):
# user "unprivileged_user", "unprivileged_group" # Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up. app_root = File.expand_path('.') working_directory app_root #"/path/to/app/current" # available in 0.94.0+ # listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
#listen "/path/to/.unicorn.sock", :backlog => 64
#listen "#{app_root}/tmp/sockets/unicorn.sock", :backlog => 64
listen "/tmp/guorj_unicorn.sock", :backlog => 64
listen 8081, :tcp_nopush => false # true # nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 120 # # feel free to point this anywhere accessible on the filesystem
#pid "/path/to/app/shared/pids/unicorn.pid"
#pid "#{app_root}/tmp/pids/unicorn.pid"
pid "/tmp/guorj_unicorn.pid" # By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
#stderr_path "/path/to/app/shared/log/unicorn.stderr.log"
#stdout_path "/path/to/app/shared/log/unicorn.stdout.log"
stderr_path "#{app_root}/log/unicorn.stderr.log"
stdout_path "#{app_root}/log/unicorn.stdout.log" # combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true # Enable this flag to have unicorn test client connections by writing the
# beginning of the HTTP headers before calling the application. This
# prevents calling the application for connections that have disconnected
# while queued. This is only guaranteed to detect clients on the same
# host unicorn runs on, and unlikely to detect disconnects even on a
# fast LAN.
# check_client_connection false before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect! # The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house
# twice as many worker_processes as you have configured.
#
# # This allows a new master process to incrementally
# # phase out the old master process with SIGTTOU to avoid a
# # thundering herd (especially in the "preload_app false" case)
# # when doing a transparent upgrade. The last worker spawned
# # will then kill off the old master process with a SIGQUIT.
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
#
# Throttle the master from forking too quickly by sleeping. Due
# to the implementation of standard Unix signal handlers, this
# helps (but does not completely) prevent identical, repeated signals
# from being lost when the receiving process is busy.
sleep 1
end after_fork do |server, worker|
# per-process listener ports for debugging/admin/migrations
# addr = "127.0.0.1:#{9293 + worker.nr}"
# server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true) # the following is *required* for Rails + "preload_app true",
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection # if preload_app is true, then you may also want to check and
# restart any other shared sockets/descriptors such as Memcached,
# and Redis. TokyoCabinet file handles are safe to reuse
# between any number of forked children (assuming your kernel
# correctly implements pread()/pwrite() system calls)
end

然后运行unicorn

unicorn_rails -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D 

添加第二个rails项目(请自行和第一个项目的配置对比不同,被网速把激情磨灭了)

/usr/local/nginx/conf/conf.d/wguorj.conf

 upstream wguorj_rails_unicorn_server {
server unix:/tmp/wguorj_unicorn.sock fail_timeout=;
#server 127.0.0.1: fail_timeout=;
}
server {
listen ;
client_max_body_size 4G;
server_name www.guorj.com;
keepalive_timeout ;
root /home/NginxUser/www/wguorj/public;
access_log /home/NginxUser/www/wguorj/log/nginx_access.log;
error_log /home/NginxUser/www/wguorj/log/nginx_error.log;
rewrite_log on;
index index.html; location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_redirect off;
proxy_pass http://wguorj_rails_unicorn_server;
}
#Rails error pages
location =/.html {
root /home/NginxUser/www/wguorj/public;
}
}

/home/NginxUser/www/wguorj/config/unicorn.rb

 # Sample verbose configuration file for Unicorn (not Rack)
#
# This configuration file documents many features of Unicorn
# that may not be needed for some applications. See
# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
# for a much simpler configuration file.
#
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.
# can't specify rails env
rails_env = ENV["RAILS_ENV"] || "production" # Use at least one worker per core if you're on a dedicated server,
# more will usually help for _short_ waits on databases/caches.
worker_processes 4 # Since Unicorn is never exposed to outside clients, it does not need to
# run on the standard HTTP port (80), there is no reason to start Unicorn
# as root unless it's from system init scripts.
# If running the master process as root and the workers as an unprivileged
# user, do this to switch euid/egid in the workers (also chowns logs):
# user "unprivileged_user", "unprivileged_group" # Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up. app_root = File.expand_path('.') working_directory app_root #"/path/to/app/current" # available in 0.94.0+ # listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
#listen "/path/to/.unicorn.sock", :backlog => 64
#listen "#{app_root}/tmp/sockets/unicorn.sock", :backlog => 64
listen "/tmp/wguorj_unicorn.sock", :backlog => 64
listen 8082, :tcp_nopush => false # true # nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 120 # # feel free to point this anywhere accessible on the filesystem
#pid "/path/to/app/shared/pids/unicorn.pid"
#pid "#{app_root}/tmp/pids/unicorn.pid"
pid "/tmp/wguorj_unicorn.pid" # By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
#stderr_path "/path/to/app/shared/log/unicorn.stderr.log"
#stdout_path "/path/to/app/shared/log/unicorn.stdout.log"
stderr_path "#{app_root}/log/unicorn.stderr.log"
stdout_path "#{app_root}/log/unicorn.stdout.log" # combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true # Enable this flag to have unicorn test client connections by writing the
# beginning of the HTTP headers before calling the application. This
# prevents calling the application for connections that have disconnected
# while queued. This is only guaranteed to detect clients on the same
# host unicorn runs on, and unlikely to detect disconnects even on a
# fast LAN.
# check_client_connection false before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect! # The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house
# twice as many worker_processes as you have configured.
#
# # This allows a new master process to incrementally
# # phase out the old master process with SIGTTOU to avoid a
# # thundering herd (especially in the "preload_app false" case)
# # when doing a transparent upgrade. The last worker spawned
# # will then kill off the old master process with a SIGQUIT.
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
#
# Throttle the master from forking too quickly by sleeping. Due
# to the implementation of standard Unix signal handlers, this
# helps (but does not completely) prevent identical, repeated signals
# from being lost when the receiving process is busy.
sleep 1
end after_fork do |server, worker|
# per-process listener ports for debugging/admin/migrations
# addr = "127.0.0.1:#{9293 + worker.nr}"
# server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true) # the following is *required* for Rails + "preload_app true",
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection # if preload_app is true, then you may also want to check and
# restart any other shared sockets/descriptors such as Memcached,
# and Redis. TokyoCabinet file handles are safe to reuse
# between any number of forked children (assuming your kernel
# correctly implements pread()/pwrite() system calls)
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/

http://ruby-china.org/topics/10895

Nginx+Unicorn+rails多项目 http://chinacheng.iteye.com/blog/757638

Ruby-China.org 选择用 Thin 还是 Unicorn? http://ruby-china.org/topics/35

使用Unicorn替代Mongrel作为Ruby on Rails的服务器 http://www.cslog.cn/Content/unicorn-for-ruby-on-rails/

貌似要先启动nginx 然后再启动所有的unicorn 否则会有问题 不知道是不是真的 再继续观察

touch /tmp/restart.txt 重启当前的Rails项目 http://ruby-china.org/topics/1374

touch tmp/restart.txt
其实也能实现优雅的重启 Unicorn 进程。http://ruby-china.org/topics/35#reply32

注运行两个rails项目的进程

[root@GrjServer guorj]# ps aux | grep nginx
root 0.0 0.2 ? Ss : : nginx: master process /usr/local/nginx/sbin/nginx
0.0 0.3 ? S : : nginx: worker process
root 0.0 0.1 pts/ S+ : : grep nginx
[root@GrjServer guorj]# ps aux | grep unicorn
root 0.0 12.5 ? Sl : : unicorn_rails master -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D
root 0.0 12.3 ? Sl : : unicorn_rails worker[] -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D
root 0.0 12.3 ? Sl : : unicorn_rails worker[] -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D
root 0.0 12.3 ? Sl : : unicorn_rails worker[] -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D
root 0.0 12.3 ? Sl : : unicorn_rails worker[] -c /home/NginxUser/www/guorj/config/unicorn.rb -E production -D
root 1.2 12.5 ? Sl : : unicorn_rails master -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D
root 0.0 12.3 ? Sl : : unicorn_rails worker[] -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D
root 0.0 12.3 ? Sl : : unicorn_rails worker[] -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D
root 0.0 12.3 ? Sl : : unicorn_rails worker[] -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D
root 0.0 12.3 ? Sl : : unicorn_rails worker[] -c /home/NginxUser/www/wguorj/config/unicorn.rb -E production -D
root 0.0 0.1 pts/ S+ : : grep unicorn
[root@GrjServer guorj]#

Nginx + unicorn 运行多个Rails应用程序的更多相关文章

  1. 使用Nginx负载均衡搭建高性能.NETweb应用程序二

    在文章<使用Nginx负载均衡搭建高性能.NETweb应用程序一>中,让我们对Nginx有了一个初步认识,下面我们将在windows平台下面使用Nginx演示集群部署我们的web应用. 一 ...

  2. 使用Jexus服务器运行Asp.Net Core2.0程序

    前段时间写了篇关于.net core跨平台部署的文章.https://my.oschina.net/lichaoqiang/blog/1861977 主要讲述了,利用Nginx+CentOS+Supe ...

  3. Nginx+FastCGI运行原理(一)

    1 实战Nginx与PHP(FastCGI)的安装.配置与优化 1.1 什么是 FastCGI FastCGI是一个可伸缩地.高速地在HTTP server和动态脚本语言间通信的接口.多数流行的HTT ...

  4. 使用Nginx负载均衡搭建高性能.NETweb应用程序(转)

    一.遇到的问题 当我们用IIS服务器部署了一个web应用以后,当很多用户高并发访问的时候,客户端响应就会很慢,客户的体验就会很差,由于IIS接受到客户端请求的时候,就会创建一个线程,当线程达到几千个时 ...

  5. nginx中给目录增加密码保护实现程序

    一款nginx中给目录增加密码保护实现程序,可以有效的保护一些目录不被访问,有需要的朋友可参考一下. 了防止一些可能出现存在漏洞的后台脚本暴露,使用验证的方式保护这些文件所在的目录 使用apache的 ...

  6. 安装Ruby On Rails时运行“gem install rails”没有反应怎么办?

    这两天在我的mac机上安装Ruby On Rails,感觉很爽,似乎在使用一个Windows和Linux的结合体,要界面有界面,要命令行有命令行. 不过安装Ruby On Rails的过程中遇到一个问 ...

  7. nginx如何调用PHP(nginx+php运行原理)

    采用nginx+php作为webserver的架构模式,在现如今运用相当广泛.然而第一步需要实现的是如何让nginx正确的调用php.由于nginx调用php并不是如同调用一个静态文件那么直接简单,是 ...

  8. .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序

    在 Linux Ubuntu 上成功编译 dnx 之后,会在 artifacts/build/ 文件夹中生成 dnx-coreclr-linux-x64/ 与 dnx-mono/ 这2个文件夹,前者是 ...

  9. .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序

    在之前的博文中我们在 Ubuntu 上成功编译出了 dnx ,并且用它成功运行了 ASP.NET 5 示例程序.在这篇博文中我们将 Ubuntu 换成 CentOS. 目前 dnx 的编译需要用到 m ...

随机推荐

  1. Java&Android反编工具打包

    Java&Android反编工具: 1.Eclipse反编插件:安装到Eclipse后,可以简要的查看jar包中的*.class; 2.DoAPK:反编*.apk为smali和一些资源文件,可 ...

  2. .net读取异步Post的内容

    //读取微信Post过来的XML内容                 byte[] input = HttpContext.Current.Request.BinaryRead(HttpContext ...

  3. Spring整合JMS-基于activeMQ实现(二)

    Spring整合JMS-基于activeMQ实现(二) 1.消息监听器      在Spring整合JMS的应用中我们在定义消息监听器的时候一共能够定义三种类型的消息监听器,各自是MessageLis ...

  4. oracle转Mysql中,varchar2(10)和number应该转换为什么类型? (转)

    一. varchar2(10)和number应该转换为什么类型? oracle转成mysql时:varchar2(10)可以转成varchar(10)number则要看oracle中存储的具体是什么类 ...

  5. 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告

    原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...

  6. Ewebeditor最新漏洞和漏洞指数

    Ewebeditor最新漏洞和漏洞指数[收集] 来源:转载作者:佚名时间:2009-06-03 00:04:26 下面文章收集转载于网络:) 算是比較全面的ewebeditor编辑器的漏洞收集,如今的 ...

  7. 使用cm-12.0源代码编译twrp

    Select the newest branch available. This step is not necessary with Omni because Omni already includ ...

  8. js缓冲运动

    缓冲运动 现象:逐渐变慢,最后停止 原理:距离越远,速度越大 速度的计算方式: 1,速度由距离决定 2,速度=(目标值-当前值)/缩放系数 说明:速度为正负数时,也决定了物体移动的方向 示例:div缓 ...

  9. 关于程序猿怎样降低程序Bug的若干建议

    毫无疑问,程序猿是善于思考问题的一族. 一个程序的编写都是通过:思考.设计.编写.调试.測试以及执行这些主要的阶段. 但大部分程序猿都有一个问题就是不太愿意測试自己的代码. 他们草草的调式完毕以后就觉 ...

  10. 深入浅出JMS(一)——JMS简要

    假设手机只能实时通话.没有邮件和短信功能发生?一个电话回来.只是没有足够的时间去连接.然后传递这款手机的信息肯定是不接受. 么不能先将信息存下来.当用户须要查看信息的时候再去获得信息呢?伴随着这个疑惑 ...