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 ...
随机推荐
- HTML5----响应式(自适应)网页设计
第一步:在网页代码的头部,加入一行viewport元标签 <meta name="viewport" content="width=device-width, in ...
- java正則表達式的坑
java中正則表達式比較有意思,这里列举几个常见的坑 1.[]符号,中括号表示当中的数据都是或的关系 假设[\\w+]是匹配条件 abc能否够匹配的到呢? 首先\\w(注意这里没有中括号)表示a-z ...
- Linux内核——定时器和时间管理
定时器和时间管理 系统定时器是一种可编程硬件芯片.它能以固定频率产生中断.该中断就是所谓的定时器中断.它所相应的中断处理程序负责更新系统时间,还负责执行须要周期性执行的任务. 系统定时器和时钟中断处理 ...
- 基于Office 365 无代码工作流分析-需求基本分析!
客户需求分析: 嘉昊信息是一家IT创业型公司,因为公司初创,有较多的招聘员工的需求,公司近期购买了Office 365,因为招聘工作繁琐,HR人员须要做非常多反复繁琐工作,HR主管提议开发一个招 ...
- Linux date -s(转)
修改linux的时间可以使用date指令 修改日期: 时间设定成2009年5月10日的命令如下: #date -s 05/10/2009 修改时间: 将系统时间设定成上午10点18分0秒的命令如下. ...
- 使用JAVASCRIPT实现静态物体、静态方法和静态属性
Javascript语言的面向对象特征非常弱.其它面向对象语言在创建类时仅仅要使用keywordstatic就可以指定类为静态类,Javascript没有提供static这种keyword.要让Jav ...
- EHCache的使用
在开发高并发量,高性能的网站应用系统时,缓存Cache起到了非常重要的作用.本文主要介绍EHCache的使用,以及使用EHCache的实践经验.笔者使用过多种基于Java的开源Cache组件,其中包括 ...
- wpf 模拟3D效果(和手机浏览图片效果相似)(附源码)
原文 wpf 模拟3D效果(和手机浏览图片效果相似)(附源码) pf的3D是一个很有意思的东西,类似于ps的效果,类似于电影动画的效果,因为动画的效果,(对于3D基础的摄像机,光源,之类不介绍,对于依 ...
- 使用JSP实现商场购物车模块
这些日子,学习JSP,只要找到一个小模块来试试你的手: 这里是实现车模块结果页面截图: 图1. 产品显示页面 通过此页面进行产品选择.增加到购物车 图2 .购物车页面 图3 . 商品数量设置 好了,先 ...
- Silverlight的Socket通信
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPwAAAGwCAIAAAACJJ+TAAAgAElEQVR4nO2deXgT5534Rdhskme7+9