nginx映射表map解析

nginx映射表map解析

一、概述和原理

ngx_http_map_module 模块map指令声明变量,这个变量的值依赖于其他变量,类同于其他语言中的Case语句,下面是nginx官方内置example。

map $http_host $name {
    hostnames;
    default 0;
    example.com 1;
    *.example.com 1;
    example.org 2;
    *.example.org 2;
    .example.net 3;
    wap.* 4;
}

map $http_user_agent $mobile {
    default 0;
    "~Opera Mini" 1;
}

二、实践测试

本系列文章都通过自行编译tengine,并通过dso_tool新增模块开发测试。

map $uri $layer7_locate {
                default "misc";
                ~^/ws/mapapi/traffic/eta/single "eta";
                ~^/ws/mapapi/traffic/eta/message "eta";
                ~^/ws/shield "shield";
                ~^/ws/transfer/traffic "pypy_traffic";
                ~^/ws/transfer/aps/locate "pypy_traffic";
                ~^/ws/transfer/auth/aps/locate "pypy_traffic";
                ~^/ws/transfer/auth/aps/fence/status "pypy_traffic";
                ~^/ws/transfer/auth/dyui "shield";
                ~^/ws/mapapi/navigation/auto "transfer";
                ~^/ws/mapapi/navigation/bus/ext "transfer";
                ~^/ws/transfer "shield";
                ~^/ws/mapapi/poi "poi";
                ~^/ws/mapapi/geo "poi";
                ~^/ws/mapapi/recommend "poi";
                ~^/ASS "s";
                ~^/ws/valueadded/threedimension/query "s";
                ~^/ws/valueadded/pm25/info "s";
                ~^/ws/valueadded/deepinfo/mapping "s";
                ~^/ws/valueadded/deepinfo/discount_info "s";
                ~^/ws/mapapi/recommend/favor "s";
                ~^/ws/valueadded/taxi/position "s";
                ~^/ws/valueadded/alimama/poiad/info "s";
                ~^/ws/valueadded/cms/recommendtopic "s";
                ~^/web/ws “s”;
                ~^/ws/h5_log "s";
                ~^/ws/mapapi "misc";
                ~^/ws/app "misc";
                ~^/ws/sysconf "misc";
                ~^/ws/valueadded "misc";
}

如上面的语句所示,$layer7_locate这个变量的值依赖于$uri的值。
location / {
default_type text/plain;
echo url:$uri;
echo layer7_locate:$layer7_locate;
}

server中配置如上所示,测试case:

[admin@e100069205103.zmf /home/admin/cai/conf]
$curl localhost/a
url:/a
layer7_locate:misc

发表评论

电子邮件地址不会被公开。 必填项已用*标注