目标:收集用户日志

流程:

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

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

2、安装lua_nginx_module 模块

(略,自行百度)

3、编辑nginx.conf配置文件

  1. [root@master1 conf]# cat /usr/local/nginx/conf/nginx.conf
  2. worker_processes ;
  3. error_log logs/error.log;
  4. pid logs/nginx.pid;
  5.  
  6. events {
  7. worker_connections ;
  8. }
  9.  
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13.  
  14. log_format log_format '$remote_addr^A$msec^A$http_host^A$request_uri';
  15. log_format log_token '$uid_by_token^A$remote_addr^A$msec^A$http_host^A$request_uri';
  16.  
  17. sendfile on;
  18. #tcp_nopush on;
  19.  
  20. #keepalive_timeout ;
  21. keepalive_timeout ;
  22. lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;";
  23. server {
  24. listen ;
  25. server_name master1 0.0.0.0;
  26.  
  27. location /log {
  28. allow 192.168.8.0/;
  29. deny all;
  30. default_type 'text/html';
  31. access_log logs/access.log log_format;
  32. content_by_lua '
  33. ngx.say("success")
  34. ';
  35. }
  36.  
  37. location /do {
  38. #lua_code_cache off;
  39. set $uid_by_token '';
  40. default_type 'text/html';
  41. rewrite_by_lua_file "conf/do.lua";
  42. access_log logs/access.log log_token;
  43. }
  44. }
  45. }

4、编写do.lua文件

  1. [root@master1 ~]# vim /usr/local/nginx/conf/do.lua
  2.  
  3. --redis ip port pwd
  4. local ip = '192.168.0.8'
  5. local port =
  6. local pwd = ""
  7. local key_prefix = "Project:SsoToken:"
  8. ------------------
  9.  
  10. function get_uid(uinfo)
  11. local uid = ''
  12. if uinfo ~= ngx.null then
  13. local pos, _ = string.find(uinfo, '"Id":')
  14. if pos and pos> then
  15. local left = string.sub(uinfo, pos, -)
  16. local end_pos, _ = string.find(left, ",")
  17. local line
  18. if end_pos and end_pos> then
  19. line = string.sub(left, , end_pos)
  20. else
  21. end_pos, _ = string.find(left, "}")
  22. if end_pos and end_pos> then
  23. line = string.sub(left, , end_pos)
  24. end
  25. end
  26. line = string.sub(line, , -)
  27. uid = string.gsub(line, "^%s*(.-)%s*$", "%1")
  28. end
  29. end
  30. return uid
  31. end
  32.  
  33. local function close_redis(redis_instance)
  34. if not redis_instance then
  35. return
  36. end
  37. local ok,err = redis_instance:close();
  38. end
  39.  
  40. local token = ngx.var.arg_t
  41. local redis = require("resty.redis");
  42. local redis_instance = redis:new();
  43. redis_instance:set_timeout()
  44. local ok,err = redis_instance:connect(ip,port)
  45. if not ok then
  46. return close_redis(redis_instance);
  47. end
  48.  
  49. local auth,err = redis_instance:auth(pwd);
  50. local uinfo, err = redis_instance:get(key_prefix .. token)
  51. if not uinfo then
  52. return close_redis(redis_instance)
  53. end
  54. close_redis(redis_instance)
  55.  
  56. ngx.var.uid_by_token = get_uid(uinfo)
  57. ngx.say('done')

5、测试

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

服务器查看日志:

  1. [root@master1 nginx]# tail -f logs/access.log
  2. ^A192.168.8.^A1521181427.^Amaster1^A/do?t=99b61873a98742a3a29a4a6d64bc043f&en=pv&ct=&ver=&pl=pc
  3. ^A192.168.8.^A1521181462.^Amaster1^A/do?t=99b61873a98742a3a29a4a6d64bc043f&en=pv&ct=&ver=&pl=pc
  4. ^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. CentOS7──xxx is not in the sudoers file

    提示"xxx is not in the sudoers file. This incident will be reported.其中 ”XXX“是你的用户名,也就是你的用户名没有权限使用 ...

  2. animate.css 动画的使用

    $('#animatedClose').removeClass().addClass('fadeInDownBig animated').one('webkitAnimationEnd mozAnim ...

  3. MySQL_Key值(MUL、PRI、NUL)

    查询表结构: mysql> describe cc; +----------+-----------+------+-----+---------+-------+ | Field | Type ...

  4. [Offer收割] 编程练习赛63

    题目1 : 命名 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 有两个公司想要合并,第一个公司的名字是一个字符串S,第二个公司的名字是一个字符串T. 合并后的新公司是这样 ...

  5. JDK一键部署, 新添加进度条

    JDK部署, 脚本与JDK安装包放在同一目录 然后执行 source ./jdk.sh 稍等进度条100%即可 #******************************************* ...

  6. [Python数据挖掘]第7章、航空公司客户价值分析

    一.背景和挖掘目标 二.分析方法与过程 客户价值识别最常用的是RFM模型(最近消费时间间隔Recency,消费频率Frequency,消费金额Monetary) 1.EDA(探索性数据分析) #对数据 ...

  7. C#线程同步(5)- 信号量 Semaphore

    文章原始出处 http://xxinside.blogbus.com/logs/47617134.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...

  8. php登录注册

    php 登录注册 注册代码:register.php <style type="text/css"> form{ width:300px; background-col ...

  9. asp.net 获取网站根目录总结

    .CSHttpContext.Current.Server.MapPath();//所在文件夹路径System.Web.HttpContext.Current.Request.PhysicalAppl ...

  10. python:更改pip源

    windows更改pip源 cmd echo %APPDATA% 打开目录 创建文件夹pip 创建pip.ini文件 [global] timeout = 60 index-url = http:// ...