使用Unicorn将Sinatra应用部署到Nginx

将Sinatra应用写好了之后,可以使用unicorn将之部署到Nginx上,Sinatra的应用可以参考我的boilerplate:https://github.com/frederichchen/fc_sinatra_boilerplate

Sinatra平时开发的时候我用Thin来做服务器,部署的时候则使用unicorn,因此需要首先: gem install unicorn

部署的时候需要先进入Sinatra应用的根目录,在其中分别建立 tmp、tmp/sockets、tmp/pids和log 四个目录。

然后在Sinatra应用的根目录中新建一个unicorn.rb文件,内容为:

@dir = "/path/to/app/"

worker_processes 2
working_directory @dir

timeout 60

listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64

pid "#{@dir}tmp/pids/unicorn.pid"

stderr_path "#{@dir}log/unicorn.stderr.log"
stdout_path "#{@dir}log/unicorn.stdout.log"

 接下来配置Nginx,编辑其配置文件 nginx.conf ,在http部分加入如下如下:

http {
    ...
    upstream unicorn_server {
      server unix:/path/to/app/tmp/sockets/unicorn.sock
        fail_timeout=0;
    }
    server {
      listen       80;
      server_name  localhost;
      root /path/to/app/public;
      location / {
        try_files $uri @app;
      }
      location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        # pass to the upstream unicorn server mentioned above
        proxy_pass http://unicorn_server;
      }
    }
}  

启动服务的时候先运行:unicorn -c path/to/unicorn.rb -E development -D

其中-E指定环境为development,-D表示以daemon运行。

然后启动nginx服务,访问 http://localhost/ 就可以了。

要停止服务,则运行 cat /path/to/app/tmp/pids/unicorn.pid | xargs kill -QUIT

如果unicorn没有能够清理干净,则还需要执行以下两个命令:

    rm /path/to/app/tmp/sockets/unicorn.socket
    rm /path/to/app/tmp/pids/unicorn.pid

 

以上操作参考 https://github.com/sinatra/sinatra-recipes/blob/master/deployment/nginx_proxied_to_unicorn.md 。

posted on 2015-05-29 13:28 frederichchen 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/frederichchen/p/4538282.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值