nginx + unicorn 配置

1.Nginx介绍

Nginx是一个非常轻量级的HTTP服务器,Nginx,它的发音为“engine X”,
是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。

安装nginx

$ sudo apt-get install nginx

在nginx配置目录

$ cd /usr/local/nginx/conf

在这个目录下新建一个用户可执行的vhost文件夹

$ mkdir vhost
$ chmod 755 vhost
$ cd vhost
$ touch xx.conf
$ sudo vi xx.conf(如果有多个域名,多个项目,只需创建对应的.conf文件即可)

upstream APP_Name(填写你的APP Name) {
#    server unix:/tmp/unicorn_project-knowledge-sharing.sock  fail_timeout=0;
server unix:/home/用户(正在使用的计算机用户名)/APP_Name(填写你的APP Name)/unicorn.sock  fail_timeout=0;
}
server {

    listen 80;
    charset utf-8;
    server_name  xx.xx.com;
    # Application root, as defined previously
    root /home/用户(正在使用的计算机用户名)/APP_Name(填写你的APP Name)/public;

    location / {
            proxy_redirect off;

            proxy_set_header Host $host;

            proxy_set_header X-Forwarded-Host $host;

            proxy_set_header X-Forwarded-Server $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_buffering on;

            proxy_pass http://APP_Name(填写你的APP Name);
    }
}

在/usr/local/nginx/conf下的nginx.conf中添加一行

include vhost/*.conf; 

配置unicorn

Gemfile中添加 gem 'unicorn'
在config文件夹下新建unicorn.rb

module Rails
  class <<self
    def root
      File.expand_path("../..", __FILE__)
    end
  end
end
puts Rails.root
rails_env = ENV["RAILS_ENV"] || "production"

preload_app true
working_directory Rails.root
pid "#{Rails.root}/tmp/pids/unicorn.pid"
stderr_path "#{Rails.root}/log/unicorn.log"
stdout_path "#{Rails.root}/log/unicorn.log"

listen 5006(注意此处端口号不要和别的重复), :tcp_nopush => false

listen "#{Rails.root}/unicorn.sock"

worker_processes 1
timeout 60

if GC.respond_to?(:copy_on_write_friendly=)
  GC.copy_on_write_friendly = true
end

before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "#{Rails.root}/Gemfile"
end

before_fork do |server, worker|
  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
          puts "Send 'QUIT' signal to unicorn error!"
    end
  end
end

在根目录下创建start.sh和restart.sh(可执行文件)
start.sh

bundle exec unicorn_rails -c ./config/unicorn.rb  -D 

restart.sh

kill -s USR2 `cat ./tmp/pids/unicorn.pid`
chmod 777 start.sh
chmod 777 restart.sh

启动:

$ cd railsapp
$ ./start
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值