码迷,mamicode.com
首页 > 其他好文 > 详细

nginx unicorn 来运行rails

时间:2016-07-30 22:37:48      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

一、安装nginx

  sudo apt-get install nginx

  安装完成后查看一下:nginx -v

  技术分享

  说明安装成功。

  ubuntu系统里的安装目录是在/etc/nginx/下,启动程序文件在/usr/sbin/nginx

二、新建项目

  rails new app --skip-bundle

  完成后修改Gemfile文件:vim Gemfile

  把source 修改成taobao或者ruby-china的源。

  在这个文件里加入:gem ‘unicorn‘

  然后运行:bundle install

  这样项目就新建完成了。

三、配置nginx

  修改nginx的配置文件

  在http里加入:

  # App Server        
        upstream app_server{
                server unix:/path/to/.unicorn.sock fail_timeout=0;
        }  

        server {
                listen 3008;
                server_name localhost;
                root /项目路径/public;
                location / {
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header Host $http_host;
                        proxy_redirect off;
                        proxy_pass http://127.0.0.1:3001;
                }
        }

 

四、配置unicorn

  加入如下配置:

  worker_processes 4
  working_directory "/path/to/app/current"
  listen "/path/to/.unicorn.sock", :backlog => 64
  listen 3001, :tcp_nopush => true
  timeout 30
  pid "/path/to/app/shared/pids/unicorn.pid"
  preload_app true
  check_client_connection false
  run_once = true
  before_fork do |server, worker|
    defined?(ActiveRecord::Base) and
      ActiveRecord::Base.connection.disconnect!
    if run_once
      run_once = false # prevent from firing again
    end
  end

  after_fork do |server, worker|
    defined?(ActiveRecord::Base) and
      ActiveRecord::Base.establish_connection
  end

五、启动项目

  在项目下启动unicorn:

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

  启动nginx: sudo nginx, 如果 已经启动会报:

  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

  这时停掉它:sudo nginx -s stop,然后重新启动: sudo nginx

  启动完成后,在浏览器里输入:localhost:3008

  就可以看到:

  技术分享

  到这里,说明已经配置成功了。

  

 

  

nginx unicorn 来运行rails

标签:

原文地址:http://www.cnblogs.com/limx/p/5721844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!