Regex リマッププラグイン

これは正規表現をもとにしたリマップルール設定を可能にします。Apache httpd の mod_rewrite を使って達成できることと似ていますが、( まだ ) 明らかに柔軟ではなく洗練されていません。

このプラグインを使用するには、remap.config のルールを次のように設定してください。

map http://a.com http://b.com @plugin=regex_remap.so @pparam=maps.reg

ファイル名パラメーターは常に必要です。絶対パスが指定されない限り、ファイル名は Traffic Server 設定ディレクトリに対する相対パスとみなされます。

設定ファイルに記載された正規表現の一覧は順番に評価されます。正規表現がリクエスト URL にマッチすると、評価は終了し、リライトルールが適用されます。もしどの正規表現もマッチしない場合は、デフォルトの宛先 URL が適用されます ( 上記の例では http://b.com) 。

文字列 "profile" を指定した任意の引数 (@param) は regex リマップルールのプロファイリングを有効化します。

... @pparam=maps.reg @pparam=profile

プロファイリングはとても低いオーバーヘッドで、情報はログディレクトリ内にある traffic.out に出力されます。この情報は、一番よくマッチするものがファイルの前方に出てくるようにするなど、正規表現の順番を最適化するのに便利です。プロファイルの出力を強制するには次のようにしてください。

$ sudo touch remap.config
$ sudo traffic_ctl config reload

By default, this plugin operates on the post-remap URL (including any remappings done by preceding plugins in the remap rule). This behavior can be modified with the optional parameter

@pparam=[no-]pristine            [default: off]

With @pparam=pristine, the plugin will operate on the pre-remap, or pristine, URL. (But, if no regular expression in the config file is matched, the resulting URL will still be the post-remap URL.)

デフォルトでは URL のパスとクエリー文字列が正規表現のマッチに提供されます。次の任意のパラメーターがプラグインインスタンスの振る舞いを変更するために使用できます。

@pparam=[no-]method              [default: off]
@pparam=[no-]query-string        [default: on]
@pparam=[no-]matrix-parameters   [default: off]
@pparam=[no-]host                [default: off]

使用された HTTP メソッド ( 例 "GET") でマッチしたい場合は、@pparam=method オプションを使用しなければなりません。

... @pparam=maps.reg @pparam=method

これを有効化することで、マッチさせる文字列は次のようになります。

GET/path?query=bar

メソッドは常にすべて大文字で、常に後ろに一つのスペースが付きます。メソッドと URL の残りの部分 ( もしくは URI パス ) の間にスペースはありません。

デフォルトではクエリー文字列はマッチさせる文字列の一部なので、これをオフにするには 'no-query-string' オプションを使用してください。

... @pparam=maps.reg @pparam=no-query-string

You can also include the matrix parameters in the string, using the option 'matrix-parameters', e.g.

... @pparam=maps.reg @pparam=matrix-parameters

Finally, to match on the host as well, use the option 'host', e.g.

... @pparam=maps.reg @pparam=host

これを有効化することで、マッチさせる文字列は次のようになります。

//host/path?query=bar

典型的な regex はこの様になります。

^/(ogre.*)/more     http://www.ogre.com/$h/$0/$1

正規表現はスペースを含んではいけません!

正規表現のマッチが行われる際、URL パス + クエリー文字列のみがマッチされます ( いかなる任意の設定オプションも指定されていない場合) 。パスは常に "/" で始まります。右辺では評価の中で様々な置き換え文字列が使用できます。

$0     - The entire matched string
$1-9   - Regular expression groups ($1 first group etc.)
$h     - The host as used in the "to" portion of the remap rule. For a long time it was the original host header from the request.
$f     - The host as used in the "from" portion of the remap rule
$t     - The host as used in the "to" portion of the remap rule
$p     - The original port number
$s     - The scheme (e.g. http) of the request
$P     - The entire path of the request
$q     - The query part of the request
$r     - The path parameters of the request (not implemented yet)
$c     - The cookie string from the request
$i     - The client IP for this request

注釈

$0 置き換えは、正規表現のマッチに使用された文字列全体ではなく、正規表現にマッチした文字列を展開します。

remap.config を設定するのと同じようにオプションを提供することもできます。次のオプションが利用可能です。

@status=<nnn>               - Force the response code to <nnn>
@active_timeout=<nnn>       - Active timeout (in ms)
@no_activity_timeout=<nnn>  - No activity timeout (in ms)
@connect_timeout=<nnn>      - Connect timeouts (in ms)
@dns_timeout=<nnn>          - Connect timeouts (in ms)

@overridable-config=<value> - see :ref:`ts-overridable-config`

@caseless                   - Make regular expressions case insensitive
@lowercase_substitutions    - Turn on (enable) lower case substitutions

これはいくつかの URL に特定のレスポンスを強制するのに便利です。

^/(ogre.*)/bad      http://www.example.com/  @status=404

もしくは 302 リダイレクトを強制することもできます。

^/oldurl/(.*)$      http://news.example.com/new/$1 @status=302

status を 301 か 302 に設定すると 新しい URL がリダイレクト (Location:) に使用されることを強制できます。