Nginx ngx_http_browser_module模块基本指令整理

原创文章,转载请指明出处并保留原文url地址

本文主要针对nginx的ngx_http_browser_module模块做简单介绍,本文具体包括如下指令:

ancient_browser,ancient_browser_value,modern_browser,modern_browser_value

ngx_http_browser_module模块根据用户请求中"User-Agent"内容创建代表浏览器的变量

$modern_browser

若浏览器被识别为一个流行的浏览器,这个值等于指令modern_browser_value指定的值。

$ancient_browser

若浏览器被识别为一个比较旧的浏览器,这个值等于指令ancient_browser_value指定的值。

$msie

如果浏览器被识别为MSIE,这个值为1。

【如果不需要这个模块,可以在编译nginx时增加-without-http_browser_module参数】

配置实例:

为指定的浏览器指定index文件:

modern_browser_value "modern.";

modern_browser msie 5.5;

modern_browser gecko 1.0.0;

modern_browser opera 9.0;

modern_browser safari 413;

modern_browser konqueror 3.0;

index index.${modern_browser}html index.html;

将一些来自比较旧的浏览器的请求重定向到特定页面:

modern_browser msie 5.0;

modern_browser gecko 0.9.1;

modern_browser opera 8.0;

modern_browser safari 413;

modern_browser konqueror 3.0;

modern_browser unlisted;

ancient_browser Links Lynx Netscape4;

if ($ancient_browser){

rewrite  ^  /ancient.html;

}

Nginx原文:

The ngx_http_browser_module module creates variables whose values depend on the value of the “User-Agent” request header field:

$modern_browser

equals to the value set by the modern_browser_value directive, if a browser was identified as modern;

$ancient_browser

equals to the value set by the ancient_browser_value directive, if a browser was identified as ancient;

$msie

equals “1” if a browser was identified as MSIE of any version.

Example Configuration

Choosing an index file:

modern_browser_value "modern.";

modern_browser msie      5.5;

modern_browser gecko     1.0.0;

modern_browser opera     9.0;

modern_browser safari    413;

modern_browser konqueror 3.0;

index index.${modern_browser}html index.html;

Redirection for old browsers:

modern_browser msie      5.0;

modern_browser gecko     0.9.1;

modern_browser opera     8.0;

modern_browser safari    413;

modern_browser konqueror 3.0;

modern_browser unlisted;

ancient_browser Links Lynx netscape4;

if ($ancient_browser) {

rewrite ^ /ancient.html;

}

1. ancient_browser

syntax:ancient_browser   string...;
default:
context:http, server, location

在用户请求的“User-Agent”头中发现任何一个被设定的字符串被发现到,则这个浏览器被考虑为古老的浏览器。一个比较特殊的字符串是“netscape4”,它对应正则表达式“^Mozilla/[1-4] ”。

Nginx原文:

If any of the specified substrings is found in the “User-Agent” request header field, a browser will be considered ancient. The special string “netscape4” corresponds to the regular expression “^Mozilla/[1-4]”.

2. ancient_browser_value

syntax:ancient_browser_value   string;
default:ancient_browser_value 1;
context:http, server, location

为变量$ancient_browser设置一个值。

Nginx原文:

Sets a value for the $ancient_browser variables.

3. modern_browser

syntax:modern_browser  browser  version;
modern_browser  unlisted;
default:
context:http, server, location

为浏览器制定一个开始的版本号, 浏览器从这个版本号开始作为现代浏览器。这些浏览器可以是下面的一些,msie,gecko(mozilia及火狐浏览器基于这个内核),opera, safari或者konqueror等。

版本可以按照下面的格式制定:x,x.x,x.x.x 或者x.x.x.x。每个格式的最大值分别是4000, 4000.99,4000.99.99和4000.99.99.99

如果一个请求的“User-Agent”字段值没有在modern_browser 和ancient_browser指令列表中,这个设置将考虑这个浏览器作为现代的。否则没有这个设置,这个浏览器将被考虑为古老的。若是用户请求中没有 “User-Agent” 字段这被认为是不在列表中(unlisted )处理(若是明确设置了unlisted 指令, 没有ua的情况作为现代处理, 若是没有设置unlisted 指令,则这个情况作为古老的处理)。

Nginx原文:

Specifies a version starting from which a browser is considered modern. A browser can be any one of the following: msie, gecko (browsers based on Mozilla), opera, safari, or konqueror.

Versions can be specified in the following formats: X, X.X, X.X.X, or X.X.X.X. The maximum values for each of the format are 4000, 4000.99, 4000.99.99, and 4000.99.99.99, respectively.

The special value unlisted specifies to consider a browser as modern if it was not listed by the modern_browser and ancient_browser directives. Otherwise such a browser is considered ancient. If a request does not provide the “User-Agent” field in the header, a browser is treated as not being listed.

4. modern_browser_value

syntax:modern_browser_value   string;
default:modern_browser_value 1;
context:http, server, location

为$modern_browser变量设置一个值

Nginx原文:

Sets a value for the $modern_browser variables.

发表评论