User Agent

説明

nginx-http-user-agent - NGINXのネイティブの ngx_http_browser_moduleに似ていますが、もっと多くの検索オプションを提供します。

ソースリポジトリ

githubから取得してください。

構文

user_agent $variable_name {
    greedy name;

    name [([+|-]version) | (version1~version2)]  value;
}

if ($variable == value) {
    echo hello;
}

ディレクティブ

greedy

右から左へuser_agent 文字列の中のキーワードを指定します。これは効率的です。 いつものように、greedyアルゴリズムを使います。キーワードが見つかるとすぐに返るでしょう。

例 1. “Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)”, this string is MSIE’s user_agent string, we will return when we find the keyword “MSIE”. しかし現実は常にこのようなものではありません: 例 2. “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20”, This is Chrome’s user_agent. まずSafariに合致するでしょう。もしsafari を greedy と定義すると、逆順に文字列を走査します。もしキーワードが greedy であれば、キーワードが最初に合致した時にすぐに返さず、文字列を走査し続けます。

デフォルト

この変数のデフォルト値を設定します;

ブロック内のディレクティブの書式は以下のようになります:

name   version    value;
  • name - operating_system, browser, crawler などの名前。
  • version - 省略可能です。複数の書式をサポートします。
  • value - 変数に詰められる値。

例えば:

user_agent $example {

    #set default value
    default  msie;

    #define safari is greedy
    greedy  safari;

    #match exact version
    msie  6.0  1;

    #match interval
    msie  7.0~8.0  2;

    #match greater than version 9.0
    msie  9.0+  3;

    #match less than version 4.0 (include 4.0)
    msie  4.0-  4;

    #match all
    Chrome  5;
}