Nginx + unicorn 运行多个Rails应用程序
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应用程序的更多相关文章
- 使用Nginx负载均衡搭建高性能.NETweb应用程序二
在文章<使用Nginx负载均衡搭建高性能.NETweb应用程序一>中,让我们对Nginx有了一个初步认识,下面我们将在windows平台下面使用Nginx演示集群部署我们的web应用. 一 ...
- 使用Jexus服务器运行Asp.Net Core2.0程序
前段时间写了篇关于.net core跨平台部署的文章.https://my.oschina.net/lichaoqiang/blog/1861977 主要讲述了,利用Nginx+CentOS+Supe ...
- Nginx+FastCGI运行原理(一)
1 实战Nginx与PHP(FastCGI)的安装.配置与优化 1.1 什么是 FastCGI FastCGI是一个可伸缩地.高速地在HTTP server和动态脚本语言间通信的接口.多数流行的HTT ...
- 使用Nginx负载均衡搭建高性能.NETweb应用程序(转)
一.遇到的问题 当我们用IIS服务器部署了一个web应用以后,当很多用户高并发访问的时候,客户端响应就会很慢,客户的体验就会很差,由于IIS接受到客户端请求的时候,就会创建一个线程,当线程达到几千个时 ...
- nginx中给目录增加密码保护实现程序
一款nginx中给目录增加密码保护实现程序,可以有效的保护一些目录不被访问,有需要的朋友可参考一下. 了防止一些可能出现存在漏洞的后台脚本暴露,使用验证的方式保护这些文件所在的目录 使用apache的 ...
- 安装Ruby On Rails时运行“gem install rails”没有反应怎么办?
这两天在我的mac机上安装Ruby On Rails,感觉很爽,似乎在使用一个Windows和Linux的结合体,要界面有界面,要命令行有命令行. 不过安装Ruby On Rails的过程中遇到一个问 ...
- nginx如何调用PHP(nginx+php运行原理)
采用nginx+php作为webserver的架构模式,在现如今运用相当广泛.然而第一步需要实现的是如何让nginx正确的调用php.由于nginx调用php并不是如同调用一个静态文件那么直接简单,是 ...
- .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序
在 Linux Ubuntu 上成功编译 dnx 之后,会在 artifacts/build/ 文件夹中生成 dnx-coreclr-linux-x64/ 与 dnx-mono/ 这2个文件夹,前者是 ...
- .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序
在之前的博文中我们在 Ubuntu 上成功编译出了 dnx ,并且用它成功运行了 ASP.NET 5 示例程序.在这篇博文中我们将 Ubuntu 换成 CentOS. 目前 dnx 的编译需要用到 m ...
随机推荐
- cocos2d-x3.0 实现HTTP请求GET、POST
HTTP请求实现 把以下代码拷贝到新创建的project中就能看到效果 HelloWorldScene.h #include "cocos2d.h" /*记得要引头文件*/ #in ...
- PHP_保留两位小数而且四舍五入_保留两位小数而且不四舍五入
php保留两位小数而且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); php保留两位小数而且不四舍五入 $num = ...
- 样品GA的良好理解
遗传算法演示样本手册模拟 为了更好地理解遗传算法的计算过程,法的各 个主要运行步骤. 例:求下述二元函数的最大值: (1) 个体编码 遗传算法的运算对象是表示个体 ...
- WebService(2)-XML系列之Java和Xml之间相互转换
源代码下载:链接:http://pan.baidu.com/s/1ntL1a7R password: rwp1 本文主要讲述:使用jaxb完毕对象和xml之间的转换 TestJava2xml.java ...
- 重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresenter
原文:重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresente ...
- git tag使用
#git tag command git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] ...
- effective c++ 条款11 Handle assignment to self in operator=
赋值给自己,听起来有些不可思议,但是却要引起重视,它很容易把自己隐藏起来. 例如 1 a[i]=a[j]; 如果 i, j的值一样? 2 *px=*py; 如果px py指向同一个object 3 ...
- WPF 绑定
WPF里分三种Binding:Binding, PriorityBinding, MultiBinding,这三种Binding的基类都是BindingBase,而BindingBase又继承于Mar ...
- 关于.net MVC5+EF6 网站部署的问题
创建mvc web application,采用code first 的方式,MVC5,EF6.0 整了一个网站.开发完之后.直接publish.就这样部署到服务器上了. 在使用过程中发现,网站打开的 ...
- drools6 基本使用 -- 2
续drools6 基本使用1 http://blog.csdn.net/cloud_ll/article/details/26979355 8. 创建src/main/test folder.把dro ...