nginx ve unicorn'la rack uygulamalarını koşturmak
DESCRIPTION
Unix soket'ini Nginx sunucu ile entegre ederek Rack uygulamalarını çalıştırmak ve sunucuda koşturmak. Ruby ve Sinatra örnekleriTRANSCRIPT
Rack Uygulamalarını NginX ve Unicorn’la
Koşturma
Uğur “vigo” ÖzyılmazelwebBox.io kurucu ortağı, Yazılım Geliştirici
https://twitter.com/vigobronx
https://github.com/vigo
http://ugur.ozyilmazel.com
https://speakerdeck.com/vigo
Ruby için web sunucusu arayüzüdür.
RESPONSE ve REQUEST için http wrapper’dır.
Web sunucusu ve Ruby’nin kolay iletişim kurmasını sağlar
Ara katmanlar yardımıyla ekstra kolaylıklar sağlar
Ruby Uygulama
Web Sunucusu
Ruby Uygulama
Web Sunucusu
{ Middleware }
{ Mongrel, WEBrick, CGI, Thin … }
{ Sinatra, Rails, Ramaze … }
`call` metoduna cevap veren Ruby nesnesi yeterlidir!
return [ status, headers, body ]
require 'rack' !class MyApplication def call(env) h = {"Content-Type" => "text/html; charset=utf-8"} [200, h, ["Merhaba Dünya"]] end end !run MyApplication.new
Basit bir Rack uygulaması
Web Sunucusu
Mongrel
EventedMongrel
SwiftipliedMongrel
WEBrick
FCGI
CGI
SCGI
LiteSpeed
Thin
{Rack Handlers *
Ebb
Fuzed
Glassfish v3
Phusion Passenger
Puma
Rainbows!
Reel
Unicorn
unixrack
uWSGI
Zbatery
{Middleware
JSON-P
Rack::Protection
Rack::Cache
Rack::Config
Rack::Debug
Rack::RespondTo
Rack::GoogleAnalytics
Rack::Throttle
Rack::LinkedData
Rack::Attack
…
Camping
Coset
Espresso
Halcyon
Mack
Maveric
Merb
Racktools::SimpleApplication
Ramaze
Ruby on Rails
Rum
Sinatra
Sin
Vintage
Waves
Wee
…
Ruby Uygulama +
HTTP ve Reverse Proxy sunucusu (Web sunucusu)
Caching ve Load-Balancing özellikleri bulunuyor
Düşük hafıza kullanımı ve performanslı olması tercih sebebi!
Event Driven Architecture
Ana İşlem (Main Process)
İşçi (Worker) İşçi (Worker) İşçi (Worker)
Nginx Kurulum
# Ubuntu 12.04 <= add-apt-repository $ sudo aptitude install python-software-properties !$ sudo add-apt-repository ppa:nginx/stable $ sudo aptitude update $ sudo aptitude install nginx !# servisi başlatır! $ sudo service nginx start
Unicorn
Http Server (Ruby gem’i)
NginX gibi worker’lar kullanıyor
Request’i Unix socket’ine yönlendiriyor
Kendi başına otomatik olarak worker’ları yumurtluyor ve organize ediyor!
Kurulum
$ gem install unicorn
source "https://rubygems.org" gem 'unicorn'
Gemfile
# encoding: utf-8 require 'sinatra/base' !class MyApplication < Sinatra::Base get '/' do "Merhaba Dünya!" end end
application.rb
@dir = “/path/to/tmp/" !worker_processes 2 working_directory @dir !timeout 30 listen "#{@dir}sockets/unicorn.sock", :backlog => 64 pid_file = "#{@dir}pids/unicorn.pid" old_pid = "#{pid_file}.oldbin" pid pid_file stderr_path "#{@dir}log/unicorn.stderr.log" stdout_path "#{@dir}log/unicorn.stdout.log" preload_app true !before_exec do |server| ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__)) end !before_fork do |server, worker| if File.exists?(old_pid) && server.pid != old_pid begin Process.kill("QUIT", File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH # pass end end end
unicorn.rb
upstream unicorn_mysocket { server unix:/path/to/tmp/sockets/unicorn.sock fail_timeout=0; } !server { server_name localhost; listen 8080; root /path/to/project; client_max_body_size 4G; keepalive_timeout 5; ! 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; proxy_pass http://unicorn_mysocket; } }
nginx.conf
upstream unicorn_mysocket { server unix:/path/to/tmp/sockets/unicorn.sock fail_timeout=0; } !server { server_name localhost; listen 8080; root /path/to/project; client_max_body_size 4G; keepalive_timeout 5; ! 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; proxy_pass http://unicorn_mysocket; } }
nginx.conf
# encoding: utf-8 require "rubygems" require "sinatra" !require File.expand_path '../application.rb', __FILE__ run MyApplication
$ bundle exec unicorn -c $PWD/unicorn.rb -D $PWD/config.ru
config.ru
$ ps aux | grep unicorn
worker
master1396 $ kill 1396
VİDEO
worker işlemlerinin ölçeklenmesi, işlemci (CPU) ve hafızayla doğru orantılıdır.
Yavaş istemcilerle (slow-client) NginX gibi sunucular uğraşmalı unicorn değil!
Ölçeklendirmede asıl düşünülmesi gereken back-end kısmı unicorn değil!
Kaynaklar
http://rack.github.io/
http://nginx.org/
http://unicorn.bogomips.org/
http://sinatrarb.com
http://vagrantup.com
https://github.com/vigo/owg2013-rack-unicorn-sinatra
http://webbox.io
http://blog.webbox.io
http://twitter.com/webboxio
https://github.com/webBoxio
http://facebook.com/webbox.io