设为首页 收藏本站
查看: 811|回复: 0

[经验分享] Nginx+Unicorn+rails多项目

[复制链接]

尚未签到

发表于 2016-12-23 09:40:41 | 显示全部楼层 |阅读模式
使用nginx搭配unicorn来启动多个工程,同时还要照顾过去那些使用lighttpd启动的项目
一、nginx的安装配置
tar zxvf nginx-0.7.67.tar.gz
cd nginx-0.7.67/
./configure
make
make install

默认在/usr/local/nginx/sbin下
执行命令
cd /usr/local/nginx/sbin
./nginx
如是想更换端口 编辑配置文件/usr/local/nginx/conf/nginx.conf 中的配置即可
下面要配置一个工程,通过nginx和unicorn来启动
  创建这个目录待用/tmp/nginx/sockets/
  在nginx配置文件中添加一个
  upstream config_manager {
server unix:/tmp/nginx/sockets/config_manager_unicorn.sock fail_timeout=0;
}

  server中添加
server {
listen       80;
server_name  ***.******.com;
………
……
location ~ ^/config {                 (注:这是一个正则表达式 匹配浏览器地址访地址 的path,这里以config开头的path在这里面处理,也可以直接写成 location /config)
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://config_manager;(注:这个config_manager 对应上面的upstream)
}

二、安装unicorn并在项目中添加对应的配置
安装很简单,直接
gem install unicorn

  在工程里面 配置路径下添加(这个是github的配置文件,有很多没用的都贴出来了)
  unicorn.rb
  内容如下:
# unicorn_rails -c /data/github/current/config/unicorn.rb -E production -D
rails_env = ENV['RAILS_ENV'] || 'production'
# 16 workers and 1 master
worker_processes (rails_env == 'production' ? 16 : 4)
# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true
# Restart any workers that haven't responded in 30 seconds
timeout 30
# Listen on a Unix data socket
!!!!!!!!!!!!!!!!!!!!!!!=========下面这个地址要与nginx.conf的upstream相对应
listen '/tmp/nginx/sockets/config_manager_unicorn.sock', :backlog => 2048
##
# REE
# http://www.rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end

before_fork do |server, worker|
##
# When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
# immediately start loading up a new version of itself (loaded with a new
# version of our app). When this new Unicorn is completely loaded
# it will begin spawning workers. The first worker spawned will check to
# see if an .oldbin pidfile exists. If so, this means we've just booted up
# a new Unicorn and need to tell the old one that it can now die. To do so
# we send it a QUIT.
#
# Using this method we get 0 downtime deploys.
old_pid = RAILS_ROOT + '/tmp/pids/unicorn.pid.oldbin'
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
##
# Unicorn master loads the app then forks off workers - because of the way
# Unix forking works, we need to make sure we aren't using any of the parent's
# sockets, e.g. db connection
!!!!!!!!!!!!!!!!!!!=====注释掉了第二行,主要是因为没有开启相应的memcache服务之类======!!!!!!!!!!!!!!!
ActiveRecord::Base.establish_connection
#CHIMNEY.client.connect_to_server
# Redis and Memcached would go here but their connections are established
# on demand, so the master never opens a socket

##
# Unicorn master is started as root, which is fine, but let's
# drop the workers to git:git
begin
uid, gid = Process.euid, Process.egid
user, group = 'root', 'root'
target_uid = Etc.getpwnam(user).uid
target_gid = Etc.getgrnam(group).gid
worker.tmp.chown(target_uid, target_gid)
if uid != target_uid || gid != target_gid
Process.initgroups(user, target_gid)
Process::GID.change_privilege(target_gid)
Process::UID.change_privilege(target_uid)
end
rescue => e
if RAILS_ENV == 'development'
STDERR.puts "couldn't change user, oh well"
else
raise e
end
end
end


配置完成后 访问http://***.***.com/config,会出现config路由不存在,可在工程项目中的config/environment.rb中添加这么一句
ENV['RAILS_RELATIVE_URL_ROOT'] = "/config"

再放个简约的配置
worker_processes 3
listen '/tmp/nginx/sockets/*****.sock', :backlog => 2048
timeout 30
preload_app true
before_fork do |server, worker|
old_pid = pid_file + '.oldbin'
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end

配置完成之后,打开工程目录后,运行:unicorn_rails -c config/unicorn.rb -E production

三、nginx反向代理lighttpd
现在要改用 nginx 做 web server,但是有一些老项目用的 lighttpd。
可以用 nginx 反向代理功能,把一些请求叫给 lighttpd 处理
1 修改 lighttpd 的配置文件
# /etc/lighttpd/lighttpd.conf
# 只允许本机访问  
server.bind = "localhost"  
# 从81端口启动  
server.port                = 81  

2 修改 nginx 的配置文件
# /usr/local/nginx/conf/nginx.conf
http {  
# 新的项目 对应 new.domain.com 域名  
server {  
listen          80;  
server_name     new.domain.com;  
access_log      logs/domain1.access.log main;  
index index.html;  
root  /var/www/domain1.com/htdocs;  
}  
# 旧的项目 对应 old.domain.com 域名  
server {  
listen 80;  
server_name   old.domain.com;  
location / {  
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
# 转发给 81 端口的 lighttpd 处理  
proxy_pass      http://127.0.0.1:81;  
}  
}  
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-318265-1-1.html 上篇帖子: 转.nginx 源代码分析 下篇帖子: 搭建Nginx+Java环境(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表