Accept cookies for analytics, social media, and advertising, or learn more and adjust your preferences. These cookies are on by default for visitors outside the UK and EEA. Privacy Notice.
nginx-http-user-agent - NGINXのネイティブの ngx_http_browser_moduleに似ていますが、もっと多くの検索オプションを提供します。
user_agent $variable_name {
greedy name;
name [([+|-]version) | (version1~version2)] value;
}
if ($variable == value) {
echo hello;
}
右から左へuser_agent 文字列の中のキーワードを指定します。これは効率的です。 いつものように、greedyアルゴリズムを使います。キーワードが見つかるとすぐに返るでしょう。
例 1. “Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)”、この文字列は MSIE の user_agent 文字列です。キーワード “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”、これは Chrome の user_agent です。まずSafariに合致するでしょう。もしsafari を greedy と定義すると、逆順に文字列を走査します。もしキーワードが greedy であれば、キーワードが最初に合致した時にすぐに返さず、文字列を走査し続けます。
この変数のデフォルト値を設定します;
ブロック内のディレクティブの書式は以下のようになります:
name 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;
}