Rails Deployment with NginX
-
Upload
stoyan-zhekov -
Category
Technology
-
view
9.203 -
download
5
description
Transcript of Rails Deployment with NginX
![Page 1: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/1.jpg)
Rails Deployment with NginX
完全な rails スタックの探求
![Page 3: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/3.jpg)
Deploying Rails
➲ Deployment て何?➲ Too many changes last years➲ お金の話
● EngineYard: $299/mo per slice● RailsMachine: $245/mo + $60 setup
![Page 4: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/4.jpg)
奇本が変わらない
➲ フロントエンドの web サーバ● 静的リクエストの処理
➲ バックエンドのアプリケーションサーバ● 動的リクエストの処理
➲ デタベース
![Page 5: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/5.jpg)
奇本が変わらない (2)
image by Ezra Zygmuntowicz
![Page 6: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/6.jpg)
History Lesson
➲ Apache + CGI➲ Apache + FastCGI➲ Lighttpd + FastCGI➲ Lighttpd + SCGI➲ Litespeed➲ etc. etc. (mod_fcgi, mod_ruby)➲ Mongrel
![Page 7: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/7.jpg)
Enter Mongrel
![Page 8: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/8.jpg)
Mongrel て何 ?
image by Ezra Zygmuntowicz
Mongrel は犬の雑種
![Page 9: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/9.jpg)
Mongrel て何 ? (2)
flickr image
Mongrel は犬の雑種
![Page 10: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/11.jpg)
Why Mongrel (and HTTP)?
➲ HTTP は良く知られているプロトコル➲ Mongrel はセットアップが容易➲ 高速➲ 管理が容易 (monit)➲ Scale が 容易 (TCP/IP)
![Page 12: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/12.jpg)
動的リクエスト
➲ 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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/13.jpg)
Mutex Lock around the Dispatcher
image by Ezra Zygmuntowicz
![Page 14: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/14.jpg)
どうしよう?
![Page 15: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/15.jpg)
プロセスとともに拡張
➲ mongrel_cluster➲ ロードバランサ
![Page 16: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/16.jpg)
ロードバランサ
➲ pen (no SSL)➲ pound (restart to reload the config)➲ balance➲ HAproxy➲ LiteSpeed ロードバランサ ($1299)
![Page 17: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/17.jpg)
一般的な問題は?
➲ 静的ファイルを提供しない➲ ロードがバックエンドにシフトする (bad)
![Page 18: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/18.jpg)
静的リクエスト
➲ JavaScript bigger and bigger (AJAX)–➲ Multimedia video, images–➲ Rails caching - .html➲ Distributed assets - assets%i.dot.com
![Page 19: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/19.jpg)
フロントエンドの web サーバ
➲ Apache VPS– の RAM は不十分➲ Lighttpd – 高負荷ではメモリリーク➲ Litespeed – 同時リクエスト数上限は 300
![Page 20: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/21.jpg)
問題
➲ 単独プロジェクト➲ ロシア語を読み書きできるか➲ CGI サポートなし
![Page 22: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/22.jpg)
なぜ NginX?
➲ パフォーマンスがいい➲ 稼働時のメモリ使用量が小さい (VPS)➲ proxy でメモリリークは無い➲ 名前ベース、 IPベースのバーチャルサーバ➲ PUT, DELETE, MKCOL, COPY and MOVE➲ Modular➲ FLV streaming もできる
![Page 23: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/23.jpg)
パフォーマンスがいい
➲ 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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/24.jpg)
High availability
➲ unix による管理● kill -HUP reload the config–● kill -USR2 BINARY RELOAD–
![Page 25: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/28.jpg)
Config: OS tuning
user www-data;worker_processes 1;
events { worker_connections 1024; use epoll;}
![Page 29: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/30.jpg)
Config: server blockserver { listen 80; name s1.example.com;
location / {
}}
server { listen 80; name s2.example.com; ...}
![Page 31: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/31.jpg)
Config: location blog
location / { ... proxy_pass http://mybackends; ...}
![Page 32: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/32.jpg)
おもろいもの
➲ Virtual SSI● <!--# include virtual=”/foo” -->
➲ Mirror on demand➲ memcached module
![Page 33: Rails Deployment with NginX](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/35.jpg)
これから
➲ 組合せ● 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](https://reader034.fdocuments.net/reader034/viewer/2022052209/555823b8d8b42a5e468b50ed/html5/thumbnails/36.jpg)
質問