您的位置:首页 > 运维架构 > Nginx

使用Unicorn将Sinatra应用部署到Nginx

2015-05-29 13:28 751 查看
将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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: