Mongrel + Apache 2.0 (mod_proxy) をサブディレクトリで

2007/03/12

これまで Rails で作ったアプリケーションは Apache 2.0 + FastCGI で動かしていましたが、開発環境では Mongrel を利用しているので、できれば Mongrel で運用したいと思っていました。ちなみに Mongrel とは

Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI. It is framework agnostic and already supports Ruby On Rails, Og+Nitro, Camping, and IOWA frameworks.

です。

先ごろ Mongrel もバージョンも 1.0 を超えたので、Apache と組み合わせて運用に使ってみることにしました。環境は以下の通りです。

  • CentOS 4.4
  • Apache 2.0.52 (CentOS Package)
  • Mongrel 1.0.1

要件は

  • Rails アプリケーションは Mongrel で提供する
  • 複数の Rails アプリケーションを同時に提供する
  • 複数の Rails アプリケーションを同一ドメインの別ディレクトリで提供する(バーチャルホストは利用しない)
  • Rails アプリケーション以外は Apache で提供する

になります。

構成としては、Apache をリバースプロキシとして動作させ、Rails アプリケーションへのリクエストを Mongrel にプロキシします。以下では、

の 2 つの Rails アプリケーションを提供するものとしています。

1. Mongrel を動かす

まず、RubyGems で Mongrel をインストールします。

# gem install mongrel -y

Mongrel を動かします。

# cd $APP1_DIR
# mongrel_rails start -d -p 3001 -e production --prefix /app1
# cd $APP2_DIR
# mongrel_rails start -d -p 3002 -e production --prefix /app2

–prefix オプションを指定すると、指定したプレフィックス付きで Rails アプリケーションにアクセスすることができるようになります。この場合、http://example.com:3001/app1 や http://example.com:3002/app2 で各アプリケーションにアクセスできるようになります。

2. Apache をリバースプロキシにする

Apache をリバースプロキシにするために、mod_proxy を利用します。ここでは、Apache は CentOS のパッケージを利用しているため、他の環境の場合、Apache の設定ファイル名などは適宜読み替える必要があります。

# vim /etc/httpd/conf.d/proxy.conf
ProxyRequests Off
<Proxy *>
  Order deny,allow
  Allow from all
</Proxy>
ProxyPass /app1 http://localhost:3001/app1
ProxyPassReverse /app1 http://localhost:3001/app1
ProxyPass /app2 http://localhost:3002/app2
ProxyPassReverse /app2 http://localhost:3002/app2

これで Apache をを再起動すると、/app1 および /app2 へのアクセスが各アプリケーションの Mongrel へプロキシされるようになりました。

コメントを残す