第14章 仮想ホストの高度なテクニック

この章では仮想ホストを使用してサービスを柔軟に構成する複数の方法について説明します。

仮想ホストは一般的にオリジンサーバーのDomainまたはIPリストと1:1で構成されます。 しかし状況に応じて代表仮想ホストを複数のサブ仮想ホストに分岐したり反対に独立した複数の仮想ホストを一つのサービスとして提供する場合も発生します。 各機能に応じて クライアント統計 / Accessログ のポリシーが異なる場合があることに注意する必要があります。

URL前処理

正規表現 使用して要求されたURLを変更します。 URL前処理が設定されている場合はすべてのクライアントの要求(HTTPまたはFile I / O)は必ずURL Rewriterを経る。

../_images/urlrewrite1.png

URL Rewriterを経由して仮想ホストにアクセスします。

もしURL RewriterによってアクセスしようとするHostの名前が変更された場合クライアントのHTTP要求のHostヘッダが変更されたとみなされます。 URLの前処理は仮想ホストの設定(vhosts.xml)に設定します。 ほとんどの設定は仮想ホストに依存しますがURL前処理の場合クライアントが要求したHostの名前を変更することができますので仮想ホストと同じレベルに設定します。

# vhosts.xml

<Vhosts>
   <Vhost ...> ... </Vhost>
   <Vhost ...> ... </Vhost>
   <URLRewrite ...> ... </URLRewrite>
   <URLRewrite ...> ... </URLRewrite>
</Vhosts>

マルチに設定することができ順次正規表現に一致有無を比較します。

# vhosts.xml - <Vhosts>

<URLRewrite AccessLog="Replace">
    <Pattern>www.exmaple.com/([^/]+)/(.*)</Pattern>
    <Replace>#1.exmaple.com/#2</Replace>
</URLRewrite>
  • <URLRewrite>
URL前処理を設定します。

AccessLog (デフォルト: Replace) 属性はAccessログに記録されるURLを設定します。 Replace の場合変換後のURL(/logo.jpg)を Pattern の場合変換前のURL(/baseball/logo.jpg)をAccessログに記録します。

  • <Pattern> マッチングさせるパターンを設定します。 一つのパターンは()括弧を使用して表現されます。
  • <Replace> 変換形式を設定します。 一致したパターンには#1#2のように使用することができます。 #0は要求URL全体を意味します。 パターンは最大9個(#9)まで指定することができます。

スループットは 第10章 モニタリング&統計 で提供され URL前処理成功 でも確認することができます。 URL前処理は TrimmingMP4 HLS などの他の機能と組み合わせて表現を簡単にします。

# vhosts.xml - <Vhosts>

<URLRewrite>
    <Pattern>example.com/([^/]+)/(.*)</Pattern>
    <Replace>example.com/#1.php?id=#2</Replace>
</URLRewrite>
// Pattern : example.com/releasenotes/1.3.4
// Replace : example.com/releasenotes.php?id=1.3.4

<URLRewrite>
    <Pattern>example.com/download/(.*)</Pattern>
    <Replace>download.example.com/#1</Replace>
</URLRewrite>
// Pattern : example.com/download/1.3.4
// Replace : download.example.com/1.3.4

<URLRewrite>
    <Pattern>example.com/img/(.*\.(jpg|png).*)</Pattern>
    <Replace>example.com/#1/STON/composite/watermark1</Replace>
</URLRewrite>
// Pattern : example.com/img/image.jpg?date=20140326
// Replace : example.com/image.jpg?date=20140326/STON/composite/watermark1

<URLRewrite>
    <Pattern>example.com/preview/(.*)\.(mp3|mp4|m4a)$</Pattern>
    <Replace><![CDATA[example.com/#1.#2?&end=30&boost=10&bandwidth=2000&ratio=100]]></Replace>
</URLRewrite>
// Pattern : example.com/preview/audio.m4a
// Replace : example.com/audio.m4a?end=30&boost=10&bandwidth=2000&ratio=100

<URLRewrite>
    <Pattern>example.com/(.*)\.mp4\.m3u8$</Pattern>
    <Replace>example.com/#1.mp4/mp4hls/index.m3u8</Replace>
</URLRewrite>
// Pattern : example.com/video.mp4.m3u8
// Replace : example.com/video.mp4/mp4hls/index.m3u8

<URLRewrite>
    <Pattern>example.com/(.*)_(.*)_(.*)</Pattern>
    <Replace>example.com/#0/#1/#2/#3</Replace>
</URLRewrite>
// Pattern : example.com/video.mp4_10_20
// Replace : example.com/example.com/video.mp4_10_20/video.mp4/10/20

注釈

次のように類似したサブドメインがある場合は注意する必要があります。

image.example.com
myimage.example.com

正規表現では、image.exampe.comにパターンを作成した場合myimage.example.comもパターンと一致するものとみなされる。 これを防止するために、先頭に文字なしを ^ で表記ヘジュオヤimage.example.comのみマッチングさせることができる。

<URLRewrite>
   <Pattern>^image.example.com/img/(.*\.(jpg|png).*)</Pattern>
   <Replace>image.example.com/#1/STON/composite/watermark1</Replace>
</URLRewrite>

パターンの表現にXMLの5つの特殊文字( " & ' < > )が入る場合は<![CDATA [... ]]>で囲んで正しく設定されている。 第13章 WM(Web Management) を使用して設定するとすべてのパターンはCDATAとして処理されます。

Facade仮想ホスト

<Alias> は仮想ホストの別名だけを追加しているので統計とログが分離されない。 仮想ホストは共有がドメインに基づいて クライアント統計Accessログ を分離したい場合Facade仮想ホストを設定します。

../_images/adv_vhost_facade.png

facadeは統計とログを収集します。

# vhosts.xml - <Vhosts>

<Vhost Name="example.com">
   ...
</Vhost>

<Vhost Name="another.com" Status="facade:example.com">
   ...
</Vhost>

Status 属性の値を facade: + 仮想ホスト に設定します。 例の場合 クライアント統計Accessログ はexample.comではなくクライアントが要求したドメインであるanother.comに収集されます。

Sub-Path指定

仮想ホストでパス別に他の仮想ホストが処理するように設定することができます。

../_images/adv_vhost_subpath.png

統計/ログは要求を最終処理した各仮想ホストに記録されます。

# vhosts.xml - <Vhosts>

<Vhost Name="sports.com">
  <Sub Status="Active">
    <Path Vhost="baseball.com">/baseball/<Path>
    <Path Vhost="football.com">/football/<Path>
    <Path Vhost="photo.com">/*.jpg<Path>
  </Sub>
</Vhost>

<Vhost Name="baseball.com" />
<Vhost Name="football.com" />
<Vhost Name="photo.com" />
  • <Sub> パスまたはパターンが一致するとその要求を別の仮想ホストに送る。 一致しない場合のみ現在の仮想ホストが処理します。

    • Status (デフォルト: Active) Inactiveの場合は無視します。

    • <Path> クライアントが要求したURIとパスが一致した場合 Vhost にその要求を送る。 値はパスまたはパターンのみ可能です。

      <Path Vhost="baseball.com">baseball<Path>
      <Path Vhost="photo.com">*.jpg<Path>
      

      上記のように入力してもそれぞれ/ baseball /と/*.jpgとして認識されます。

たとえばクライアントが次のようにリクエストした場合その要求は仮想ホストのfootball.comが処理します。

GET /football/rank.html HTTP/1.1
Host: sports.com

Redirect追跡

オリジンサーバーでRedirect系(301、302、303、307)で応答する場合Locationヘッダを追跡してコンテンツを要請します。

../_images/conf_redirectiontrace.png

クライアントはRedirectかどうかを知らない。

# server.xml - <Server><VHostDefault><OriginOptions>
# vhosts.xml - <Vhosts><Vhost><OriginOptions>

<RedirectionTrace>OFF</RedirectionTrace>
  • <RedirectionTrace>
    • OFF (デフォルト) 3xx 応答で保存されます。
    • ON Locationヘッダに記載されアドレスからコンテンツをダウンロードします。 形式に合わない場合Locationヘッダがない場合は動作しません。 無限Redirectされることを防止するため1回だけ追跡します。