Rails Deployment with NginX

36
Rails Deployment with NginX 完全な rails スタックの探求

description

The quest for the perfect Rails stack

Transcript of Rails Deployment with NginX

Page 1: Rails Deployment with NginX

Rails Deployment with NginX

完全な rails スタックの探求

Page 2: Rails Deployment with NginX

自己紹介

➲ 名前: Stoyan Zhekov➲ ブルガリア人➲ 子供は男三人➲ web: http://zhekov.net/

Page 3: Rails Deployment with NginX

Deploying Rails

➲ Deployment て何?➲ Too many changes last years➲ お金の話

● EngineYard: $299/mo per slice● RailsMachine: $245/mo + $60 setup

Page 4: Rails Deployment with NginX

奇本が変わらない

➲ フロントエンドの web サーバ● 静的リクエストの処理

➲ バックエンドのアプリケーションサーバ● 動的リクエストの処理

➲ デタベース

Page 5: Rails Deployment with NginX

奇本が変わらない (2)

image by Ezra Zygmuntowicz

Page 6: Rails Deployment with NginX

History Lesson

➲ Apache + CGI➲ Apache + FastCGI➲ Lighttpd + FastCGI➲ Lighttpd + SCGI➲ Litespeed➲ etc. etc. (mod_fcgi, mod_ruby)➲ Mongrel

Page 7: Rails Deployment with NginX

Enter Mongrel

Page 8: Rails Deployment with NginX

Mongrel て何 ?

image by Ezra Zygmuntowicz

Mongrel は犬の雑種

Page 9: Rails Deployment with NginX

Mongrel て何 ? (2)

flickr image

Mongrel は犬の雑種

Page 10: Rails Deployment with NginX

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/

Page 11: Rails Deployment with NginX

Why Mongrel (and HTTP)?

➲ HTTP は良く知られているプロトコル➲ Mongrel はセットアップが容易➲ 高速➲ 管理が容易 (monit)➲ Scale が 容易 (TCP/IP)

Page 12: Rails Deployment with NginX

動的リクエスト

➲ Mongrel != Rails➲ Mongrel IS thread safe➲ Rails IS NOT thread save (CGI.rb)➲ Giant Mutex Lock around the Dispatcher➲ Mongrel は時間単位で 1リクエストを提供

Page 13: Rails Deployment with NginX

Mutex Lock around the Dispatcher

image by Ezra Zygmuntowicz

Page 14: Rails Deployment with NginX

どうしよう?

Page 15: Rails Deployment with NginX

プロセスとともに拡張

➲ mongrel_cluster➲ ロードバランサ

Page 16: Rails Deployment with NginX

ロードバランサ

➲ pen (no SSL)➲ pound (restart to reload the config)➲ balance➲ HAproxy➲ LiteSpeed ロードバランサ ($1299)

Page 17: Rails Deployment with NginX

一般的な問題は?

➲ 静的ファイルを提供しない➲ ロードがバックエンドにシフトする (bad)

Page 18: Rails Deployment with NginX

静的リクエスト

➲ JavaScript bigger and bigger (AJAX)–➲ Multimedia video, images–➲ Rails caching - .html➲ Distributed assets - assets%i.dot.com

Page 19: Rails Deployment with NginX

フロントエンドの web サーバ

➲ Apache VPS– の RAM は不十分➲ Lighttpd – 高負荷ではメモリリーク➲ Litespeed – 同時リクエスト数上限は 300

Page 20: Rails Deployment with NginX

NginX: from Russia with love

➲ 名前: nginx [engine x]➲ HTTP サーバ and IMAP/POP3/SMTP proxy➲ ロシアの 20% は NginX でできてる➲ fastmail.fm➲ EngineYard

Page 21: Rails Deployment with NginX

問題

➲ 単独プロジェクト➲ ロシア語を読み書きできるか➲ CGI サポートなし

Page 22: Rails Deployment with NginX

なぜ NginX?

➲ パフォーマンスがいい➲ 稼働時のメモリ使用量が小さい (VPS)➲ proxy でメモリリークは無い➲ 名前ベース、 IPベースのバーチャルサーバ➲ PUT, DELETE, MKCOL, COPY and MOVE➲ Modular➲ FLV streaming もできる

Page 23: Rails Deployment with NginX

パフォーマンスがいい

➲ 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)–

Page 24: Rails Deployment with NginX

High availability

➲ unix による管理● kill -HUP reload the config–● kill -USR2 BINARY RELOAD–

Page 25: Rails Deployment with NginX

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; }...

Page 26: Rails Deployment with NginX

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

Page 27: Rails Deployment with NginX

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

Page 28: Rails Deployment with NginX

Config: OS tuning

user www-data;worker_processes 1;

events { worker_connections 1024; use epoll;}

Page 29: Rails Deployment with NginX

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 { ...}

Page 30: Rails Deployment with NginX

Config: server blockserver { listen 80; name s1.example.com;

location / {

}}

server { listen 80; name s2.example.com; ...}

Page 31: Rails Deployment with NginX

Config: location blog

location / { ... proxy_pass http://mybackends; ...}

Page 32: Rails Deployment with NginX

おもろいもの

➲ Virtual SSI● <!--# include virtual=”/foo” -->

➲ Mirror on demand➲ memcached module

Page 33: Rails Deployment with NginX

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; }}

Page 34: Rails Deployment with NginX

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/

Page 35: Rails Deployment with NginX

これから

➲ 組合せ● nginx + Litespeed backends

➲ バックエンドの最適化● Swiftiply Proxy: http://swiftiply.swiftcore.org/● Evented Mongrel (eventmachine)

➲ Edge Side Includes (ESI):http://www.esi.org/➲ Rails の替わりに Merb を使います

Page 36: Rails Deployment with NginX

質問