Rails Deployment with NginX 完全な rails スタックの探求
Rails Deployment with NginX
完全な rails スタックの探求
Deploying Rails
➲ Deployment て何?➲ Too many changes last years➲ お金の話
● EngineYard: $299/mo per slice● RailsMachine: $245/mo + $60 setup
奇本が変わらない
➲ フロントエンドの web サーバ● 静的リクエストの処理
➲ バックエンドのアプリケーションサーバ● 動的リクエストの処理
➲ デタベース
奇本が変わらない (2)
image by Ezra Zygmuntowicz
History Lesson
➲ Apache + CGI➲ Apache + FastCGI➲ Lighttpd + FastCGI➲ Lighttpd + SCGI➲ Litespeed➲ etc. etc. (mod_fcgi, mod_ruby)➲ Mongrel
Enter Mongrel
Mongrel て何 ?
image by Ezra Zygmuntowicz
Mongrel は犬の雑種
Mongrel て何 ? (2)
flickr image
Mongrel は犬の雑種
Mongrel て何 ? (3)
➲ HTTP サーバ and library by Zed Shaw➲ Almost pure Ruby (HTTP parser in C)➲ Modular = can have my own handlers➲ Library = can have my own framework
● Merb: http://merb.rubyforge.org/● Ramaze: http://ramaze.rubyforge.org/
Why Mongrel (and HTTP)?
➲ HTTP は良く知られているプロトコル➲ Mongrel はセットアップが容易➲ 高速➲ 管理が容易 (monit)➲ Scale が 容易 (TCP/IP)
動的リクエスト
➲ Mongrel != Rails➲ Mongrel IS thread safe➲ Rails IS NOT thread save (CGI.rb)➲ Giant Mutex Lock around the Dispatcher➲ Mongrel は時間単位で 1リクエストを提供
Mutex Lock around the Dispatcher
image by Ezra Zygmuntowicz
どうしよう?
プロセスとともに拡張
➲ mongrel_cluster➲ ロードバランサ
ロードバランサ
➲ pen (no SSL)➲ pound (restart to reload the config)➲ balance➲ HAproxy➲ LiteSpeed ロードバランサ ($1299)
一般的な問題は?
➲ 静的ファイルを提供しない➲ ロードがバックエンドにシフトする (bad)
静的リクエスト
➲ JavaScript bigger and bigger (AJAX)–➲ Multimedia video, images–➲ Rails caching - .html➲ Distributed assets - assets%i.dot.com
フロントエンドの web サーバ
➲ Apache VPS– の RAM は不十分➲ Lighttpd – 高負荷ではメモリリーク➲ Litespeed – 同時リクエスト数上限は 300
NginX: from Russia with love
➲ 名前: nginx [engine x]➲ HTTP サーバ and IMAP/POP3/SMTP proxy➲ ロシアの 20% は NginX でできてる➲ fastmail.fm➲ EngineYard
問題
➲ 単独プロジェクト➲ ロシア語を読み書きできるか➲ CGI サポートなし
なぜ NginX?
➲ パフォーマンスがいい➲ 稼働時のメモリ使用量が小さい (VPS)➲ proxy でメモリリークは無い➲ 名前ベース、 IPベースのバーチャルサーバ➲ PUT, DELETE, MKCOL, COPY and MOVE➲ Modular➲ FLV streaming もできる
パフォーマンスがいい
➲ Serious Web servers use event loops● http://www.kegel.com/c10k.html
➲ 各 OSに最適化されてる (async)● BSD kqueue–● Linux 2.6 epool–● Solaris 10 EventPorts (Joyent.com)–
High availability
➲ unix による管理● kill -HUP reload the config–● kill -USR2 BINARY RELOAD–
Coding style
if (m[1] == 'O') { if (m[0] == 'P' && m[2] == 'S' && m[3] == 'T') {
r->method = NGX_HTTP_POST; break; } if (m[0] == 'C' && m[2] == 'P' && m[3] == 'Y') {
r->method = NGX_HTTP_COPY; break; }...
NginX のインストール
Two versions: 0.5.x (stable) and 0.6.x (devel)
(Debian/Ubuntu)# apt-get install libssl-dev
http://zhware.net/code/shell/mk_nginx.sh.txt
NginX Configuration
Ezra Zygmuntowicz (merb, BackgroundDRb):
http://brainspl.at/nginx.conf.txthttp://pastie.caboo.se/84928
# gem install nginx_config_generator# generate_nginx_config –example > config.yml# generate_nginx_config config.yml nginx.conf
Config: OS tuning
user www-data;worker_processes 1;
events { worker_connections 1024; use epoll;}
Config: HTTP blockhttp { include conf/mime.types; include conf/optimize.conf;
upstream mybackends { server b1.example.com weight=5; server b2.example.com:8080; server unix:/tmp/backend3; }
server { ...}
Config: server blockserver { listen 80; name s1.example.com;
location / {
}}
server { listen 80; name s2.example.com; ...}
Config: location blog
location / { ... proxy_pass http://mybackends; ...}
おもろいもの
➲ Virtual SSI● <!--# include virtual=”/foo” -->
➲ Mirror on demand➲ memcached module
NginX Rails config
location / { # static files if (-f $request_filename) { break; } # rails caching if (-f $request_filename.html) { rewrite (.*) $1.html break; } if (!-f $request_filename) { proxy_pass http://mongrel; break; }}
Links
➲ 英語 : http://nginx.net/➲ wiki: http://wiki.codemongers.com/➲ my notes: http://wiki.zhekov.net/nginx➲ links: http://del.icio.us/zh/nginx➲ chat: #nginx on FreeNode,
http://www.lingr.com/room/nginx/
これから
➲ 組合せ● nginx + Litespeed backends
➲ バックエンドの最適化● Swiftiply Proxy: http://swiftiply.swiftcore.org/● Evented Mongrel (eventmachine)
➲ Edge Side Includes (ESI):http://www.esi.org/➲ Rails の替わりに Merb を使います
質問