• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

nginx插入lua脚本访问redis

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

目标:收集用户日志

流程:

  • 浏览器端get方法将数据传到nginx服务
  • nginx收集到数据,执行内嵌lua脚本,访问redis,根据token获得用户id
  • 将日志信息存入文件

1、nginx安装,参见:http://www.cnblogs.com/unreal/articles/7739290.html

2、安装lua_nginx_module 模块

(略,自行百度)

3、编辑nginx.conf配置文件

[root@master1 conf]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  1;
error_log  logs/error.log;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  log_format '$remote_addr^A$msec^A$http_host^A$request_uri';    
    log_format  log_token '$uid_by_token^A$remote_addr^A$msec^A$http_host^A$request_uri';    

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;";
    server {
        listen       80;
        server_name  master1 0.0.0.0;

    location /log {
        allow 192.168.8.0/24;
        deny all;
        default_type 'text/html';
        access_log logs/access.log log_format;
        content_by_lua '
                ngx.say("success")
            ';
    }

    location /do {
          #lua_code_cache off;
            set $uid_by_token '';
        default_type 'text/html';
        rewrite_by_lua_file "conf/do.lua";
        access_log logs/access.log log_token;
    }
    }
}

4、编写do.lua文件

[root@master1 ~]# vim /usr/local/nginx/conf/do.lua

--redis ip port pwd
local ip = '192.168.0.8'
local port = 6379
local pwd = "123456"
local key_prefix = "Project:SsoToken:"
------------------

function get_uid(uinfo)
    local uid = '0'
    if uinfo ~= ngx.null then  
        local pos, _ = string.find(uinfo, '"Id":')
        if pos and pos>0 then
            local left = string.sub(uinfo, pos, -1)
        local end_pos, _ = string.find(left, ",")
        local line
        if end_pos and end_pos>0 then
                line = string.sub(left, 1, end_pos)
        else
            end_pos, _ = string.find(left, "}")
            if end_pos and end_pos>0 then
                    line = string.sub(left, 1, end_pos)
        end
        end
        line = string.sub(line, 6, -2)
        uid = string.gsub(line, "^%s*(.-)%s*$", "%1")
        end
    end  
    return uid
end

local function close_redis(redis_instance)
    if not redis_instance then
        return
    end
    local ok,err = redis_instance:close();
end

local token = ngx.var.arg_t
local redis = require("resty.redis");
local redis_instance = redis:new();
redis_instance:set_timeout(2000)
local ok,err = redis_instance:connect(ip,port)
if not ok then
    return close_redis(redis_instance);
end

local auth,err = redis_instance:auth(pwd);
local uinfo, err = redis_instance:get(key_prefix .. token)  
if not uinfo then  
    return close_redis(redis_instance)  
end 
close_redis(redis_instance)

ngx.var.uid_by_token = get_uid(uinfo)
ngx.say('done')

5、测试

浏览器访问输入访问地址:http://master1/do?t=99b61873a98742a3a29a4a6d64bc043f&en=pv&ct=1521163377&ver=1&pl=pc

服务器查看日志:

[root@master1 nginx]# tail -f logs/access.log 
1002^A192.168.8.33^A1521181427.173^Amaster1^A/do?t=99b61873a98742a3a29a4a6d64bc043f&en=pv&ct=1521163377&ver=1&pl=pc
1002^A192.168.8.33^A1521181462.813^Amaster1^A/do?t=99b61873a98742a3a29a4a6d64bc043f&en=pv&ct=1521163378&ver=1&pl=pc
1002^A192.168.8.33^A1521187161.848^Amaster1^A/do?t=99b61873a98742a3a29a4a6d64bc043f&en=pv&ct=1521163378&ver=1&pl=pc

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
xLua下使用lua-protobuf发布时间:2022-07-22
下一篇:
OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap