nginx(えんじんえっくす)はロシアで開発されているwebサーバで、軽量、高速が売りのようです。もちろんvirtualhostやrewrite機能にも対応しています。今回はmongrel_clusterのフロントエンドに使ってみました。
mongrel_railsのインストールと環境構築
mongrel_railsをインストールします
gem istall mongrel_cluster --include-dependencies
設定ファイルを作ります。RAIL_ROOTで
mongrel_rails cluster::configure -e development -p 4000
とすると RAILS_ROOT/conf/mongrel_cluster.yml ができます。
--- log_file: log/mongrel.log port: "4000" environment: development pid_file: tmp/pids/mongrel.pid servers: 2
mongrel_clusterを起動します。
mongrel_rails cluster::start
nginxのインストールと環境構築
nginxは URIのrewrite機能 等に pcre を使うので、 pcre-develをインストールしておきます。
yum install pcre-devel
nginxをソースから入れます
wget http://sysoev.ru/nginx/nginx-0.5.35.tar.gz ./tar zxvf nginx-0.5.35.tar.gz ./configure --with-md5=/usr/lib --with-sha1=/usr/lib make sudo make install
設定ファイルを編集します。
vi /usr/local/nginx/conf/nginx.conf
http {
upstream mongrel_cluster {
server localhost:4000;
server localhost:4001;
}
server {
root html;
location / {
proxy_pass http://mongrel_cluster;
}
}
}設定ファイルのフォーマット等テストをします。
sudo /usr/local/nginx/sbin/nginx -t 2008/02/06 20:47:55 [info] 4249#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 2008/02/06 20:47:55 [info] 4249#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
起動します
sudo /usr/local/nginx/sbin/nginx
設定ファイルを以下のようにすれば静的ファイルは nginxが出力したりと、LVSと比べて Layer7の部分でいろいろできるかもしれないです。
server {
root html;
if (-f $request_filename) {
rewrite (.*)$1 break;
}
if (!-f $request_filename) {
proxy_pass http://mongrel_cluster;
break;
}
}-追記-
ごめんなさい 書いてから気がついたのですが
http://www.slideshare.net/zhesto/rails-deployment-with-nginx
とほとんど同じ内容でした。