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-]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

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

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

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

//host/path?query=bar

Note carefully that this includes the // prefix. This makes it possible to distinguish between the hostname and path components in cases where hostnames appear in the path. Consider, for example, a CDN that routes requests based on the origin hostname in the path, like this:

https://cdn.example.com/origin1.example.com/some/path

The // prefix allows you to write a regex that reliably identifies the actual hostname versus a hostname-like string in the path. See the example for a demonstration of how to use the host option.

A typical regex, without the above mentioned method or host options, would look like:

^/(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)

@caseless                   - Make regular expressions case insensitive
@lowercase_substitutions    - Turn on (enable) lower case substitutions
@strategy                   - Specify a strategy from strategies.yaml. "null" or "" will clear the strategy.

In addition to the options listed above, you can override any configuration variable from records.yaml on a per-rule basis. When a regex rule matches, any overridable configuration specified in that rule will be applied to the transaction. The syntax is @<config-name>=<value>, for example

@proxy.config.url_remap.pristine_host_hdr=0
@proxy.config.http.cache.http=1

These configuration overrides support integer, floating point, and string values, and will be automatically converted to the appropriate type. See TSHttpOverridableConfig for the complete list of configuration variables that can be overridden.

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

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

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

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

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

You can also combine multiple options, including overridable configs

^/(.*)?$            https://example.com/sitemaps/$1 @proxy.config.url_remap.pristine_host_hdr=0 @status=301

Examples

Example: Basic path rewriting

By default, regex_remap matches against the URL path and query string, which always starts with /. This example rewrites requests from an old URL structure to a new one:

# remap.config:
map http://www.example.com http://backend.example.com @plugin=regex_remap.so @pparam=rewrites.conf

# rewrites.conf:
^/old/(.*)$    http://backend.example.com/new/$1

A request to http://www.example.com/old/page.html will be rewritten to http://backend.example.com/new/page.html. The $1 substitution captures the first parenthesized group from the regex match.

Example: HTTP to HTTPS redirect with host matching

To redirect all HTTP requests to HTTPS while preserving the host and path, use the host option. With @pparam=host, the match string is //host/path?query (note the // prefix), so the following rule captures the entire string and redirects to HTTPS. Because the captured group includes the //, the substitution uses https: without // to avoid producing a malformed URL with four slashes:

# remap.config:
regex_map http://(.*) https://unused.invalid @plugin=regex_remap.so @pparam=redirect.conf @pparam=host @pparam=pristine

# redirect.conf:
^(.*)$    https:$1    @status=307

This will redirect any plaintext HTTP (http://) request for any host to the https:// equivalent. For instance, a request to http://example.com/path would be redirected to https://example.com/path.

Example: Method-based routing

To route requests differently based on the HTTP method, use the method option. With @pparam=method, the match string is METHOD/path?query (note no space between method and path):

# remap.config:
map http://api.example.com http://backend.example.com @plugin=regex_remap.so @pparam=api.conf @pparam=method

# api.conf:
^GET/api/v1/(.*)$     http://read-replica.example.com/api/v1/$1
^POST/api/v1/(.*)$    http://write-primary.example.com/api/v1/$1

This routes GET requests to a read replica and POST requests to the primary.