Nginx + Lua + 共享内存实现动态查询(简单例子)
2017-01-12 18:06
996 查看
Nginx 配置。
lua_package_path "/usr/local/share/luajit-2.0.2/jit?.lua;;";
lua_shared_dict devicedb 45m;
location /query {
default_type 'text/plain';
content_by_lua '
local args = ngx.req.get_uri_args()
local devicetype = args["device"]
local devicedb = ngx.shared.devicedb
local res = devicedb:get(devicetype)
ngx.say(res)
';
}
location /update {
default_type 'text/plain';
content_by_lua '
local devicedb = ngx.shared.devicedb
for item in io.lines("/usr/local/nginx-1.4.2/data/rule.txt") do
_,_,device_type, device_rule = string.find(item, "^(%a+)--(%a+)$")
devicedb:set(device_type,device_rule)
end
ngx.say("ok")
';
}
rule.txt文件格式。
SAMSUNG--samRule
APPLE--appRule
XIAOMI--xiaRule 步骤1,访问/update,更新共享内存devicedb
步骤2,访问query?device=XIAOMI,返回xiaRule
步骤3,修改rule.txt,将xiaRule改为xiaRuleaaaa
步骤4,访问/update,更新共享内存devicedb
步骤5,访问query?device=XIAOMI,返回xiaRuleaaaa
内网响应时间在5~10ms。
参考文章:
http://my.oschina.net/766/blog/158972
http://haili.me/archives/722.html
lua_package_path "/usr/local/share/luajit-2.0.2/jit?.lua;;";
lua_shared_dict devicedb 45m;
location /query {
default_type 'text/plain';
content_by_lua '
local args = ngx.req.get_uri_args()
local devicetype = args["device"]
local devicedb = ngx.shared.devicedb
local res = devicedb:get(devicetype)
ngx.say(res)
';
}
location /update {
default_type 'text/plain';
content_by_lua '
local devicedb = ngx.shared.devicedb
for item in io.lines("/usr/local/nginx-1.4.2/data/rule.txt") do
_,_,device_type, device_rule = string.find(item, "^(%a+)--(%a+)$")
devicedb:set(device_type,device_rule)
end
ngx.say("ok")
';
}
rule.txt文件格式。
SAMSUNG--samRule
APPLE--appRule
XIAOMI--xiaRule 步骤1,访问/update,更新共享内存devicedb
步骤2,访问query?device=XIAOMI,返回xiaRule
步骤3,修改rule.txt,将xiaRule改为xiaRuleaaaa
步骤4,访问/update,更新共享内存devicedb
步骤5,访问query?device=XIAOMI,返回xiaRuleaaaa
内网响应时间在5~10ms。
参考文章:
http://my.oschina.net/766/blog/158972
http://haili.me/archives/722.html
相关文章推荐
- Nginx安装配置
- [例子] nginx负载均衡搭建及测试
- Linux搭建nginx集成环境
- Cento7+Nginx 之 URL重写
- Nginx从入门到掌握【(第2节(共3节)】 推荐
- nginx配置域名,网站报错502
- Nginx配置相同端口号访问多个工程
- Nginx配置相同端口号访问多个工程
- 高性能网站架构之负载均衡 Nginx的简介
- Nginx 完整配置说明
- Ubuntu/CentOS下编译Nginx最基本参数
- 高性能网站架构之负载均衡 Nginx的安装
- nginx重新编译添加ssl模块
- nginx的安装
- Nginx负载均衡配置实例详解
- nginx重新编译添加新模块
- nginx & php-cgi 停止和启动脚本
- nginx & php-cgi 启动vb脚本
- Centos6.5 安装启动Nginx
- nginx源码解析(二)-内存池与内存管理ngx_pool_t