nginx unicorn 来运行rails

一、安装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

就可以看到:

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,910评论 0 3
  • 在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡、反向代理、代理缓存、限流等场景;而把Nginx...
    pure_adoration阅读 4,111评论 4 77
  • 人到中年,爱过、恨过,希望过、失望过,一切看淡,从容淡定。 人到中年,人未老,心已老,世间事、世间人不过如此,辉煌...
    青梅煮英雄阅读 1,119评论 5 9
  • 这是一幢老式房子,当地人就地取材,房子用的都是山上最常见的木材。这幢房子明显年老失修,到处都已裂缝,风呼呼地吹进来...
    万水千山走遍_2a6a阅读 223评论 0 0