Memory implementation of dynamic query of Nginx + Lua + sharing (example)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The Nginx configuration.

    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")            
               ';
        }

The rule.txt file type.

SAMSUNG--samRule
APPLE--appRule
XIAOMI--xiaRule


Step 1, /update access, update the shared memory devicedb

Step 2, access to the query device=XIAOMI, or xiaRule?

Step 3, modify the rule.txt, xiaRule to xiaRuleaaaa

Step 4, /update access, update the shared memory devicedb

Step 5, access to the query device=XIAOMI, or xiaRuleaaaa?


Network response time in 5~10ms.


Reference article




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Estelle at January 08, 2014 - 7:00 PM