tohokuaikiのチラシの裏

技術的ネタとか。

Apache2.4のmod_dir + mod_rewriteではまる

Railsアプリを作ってて、こんな感じでApacheでポート80番→3000番へのProxyを作ったんです。

<VirtualHost *:80>
        ServerName foo.example.jp
        CustomLog      /var/log/apache2/access_log combined
        ErrorLog       /var/log/apache2/error_log
        DirectoryIndex ""
        ProxyTimeout 8000
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyPassReverse / http://localhost:3000/
        <Location />
            RewriteEngine On
            RewriteRule ^(.+) http://localhost:3000%{REQUEST_URI} [P]
        </Location>
</VirtualHost>

ま、これは昔からよく使っていた俺的ポートProxyのconfだったのですが、これがApache2.4になった時にどうもうまく動作しない・・・。

原因

mod_dirってありますね。IndexesとDirectoryIndexの2大重要ディレクティブをを提供してくれるモジュール。Indexesは不要物で、DirectoryIndexはもはやあって当然的な使われ方してる感じで。

ところが、このDirectoryIndexが不要なんです。

DirectoryIndexが有効だと・・・・

こんな感じで変換されます。 http://foo.example.com
 ↓
http://foo.example.com/
 ↓
http://foo.example.com/index.html
 ↓
http://foo.example.com:3000/index.html

こりゃマズイ・・・。Railsはindex.htmlなんてルーティング無効だよってエラーを返す。

VirtualHostごとにDirectoryIndexをOn/Offできないか・・・とか調べたけどなさそう。 DirectoryIndex Off とかすると http://foo.example.com/Off に転送されちゃう。オイコラ。

ということで、トンチを聞かせてこんな一業を入れてみた。

<VirtualHost *:80>
        DirectoryIndex ""

空文字をIndexとするということである。

何となくこれでうまく動いてる模様。