nginx插入lua脚本访问redis
目标:收集用户日志
流程:
- 浏览器端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的更多相关文章
- 运维实践-最新Nginx二进制构建编译lua-nginx-module动态链接Lua脚本访问Redis数据库读取静态资源隐式展现
关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x0n 前言 ...
- Lua脚本在redis分布式锁场景的运用
目录 锁和分布式锁 锁是什么? 为什么需要锁? Java中的锁 分布式锁 redis 如何实现加锁 锁超时 retry redis 如何释放锁 不该释放的锁 通过Lua脚本实现锁释放 用redis做分 ...
- Lua脚本在Redis事务中的应用实践
使用过Redis事务的应该清楚,Redis事务实现是通过打包多条命令,单独的隔离操作,事务中的所有命令都会按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的命令请求所打断.事务中的命令要么全部 ...
- Redis进阶之使用Lua脚本自定义Redis命令
[本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 1.在Redis ...
- c#中用lua脚本执行redis命令
直接贴出代码,实现执行lua脚本的方法,用到的第三方类库是 StackExchange.Redis(nuget上有) 注:下面的代码是简化后的,实际使用要修改, using System; using ...
- 使用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 ...
- 使用nginx+lua脚本读写redis缓存
配置 新建spring boot项目增加redis配置 <dependency> <groupId>org.springframework.boot</groupId&g ...
- 在redis中使用lua脚本
在实际工作过程中,可以使用lua脚本来解决一些需要保证原子性的问题,而且lua脚本可以缓存在redis服务器上,势必会增加性能. 不过lua也会有很多限制,在使用的时候要注意. 在Redis中执行Lu ...
- 高并发 Nginx+Lua OpenResty系列(5)——Lua开发库Redis
Redis客户端 lua-resty-redis是为基于cosocket API的ngx_lua提供的Lua redis客户端,通过它可以完成Redis的操作.默认安装OpenResty时已经自带了该 ...
随机推荐
- CentOS7──xxx is not in the sudoers file
提示"xxx is not in the sudoers file. This incident will be reported.其中 ”XXX“是你的用户名,也就是你的用户名没有权限使用 ...
- animate.css 动画的使用
$('#animatedClose').removeClass().addClass('fadeInDownBig animated').one('webkitAnimationEnd mozAnim ...
- MySQL_Key值(MUL、PRI、NUL)
查询表结构: mysql> describe cc; +----------+-----------+------+-----+---------+-------+ | Field | Type ...
- [Offer收割] 编程练习赛63
题目1 : 命名 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 有两个公司想要合并,第一个公司的名字是一个字符串S,第二个公司的名字是一个字符串T. 合并后的新公司是这样 ...
- JDK一键部署, 新添加进度条
JDK部署, 脚本与JDK安装包放在同一目录 然后执行 source ./jdk.sh 稍等进度条100%即可 #******************************************* ...
- [Python数据挖掘]第7章、航空公司客户价值分析
一.背景和挖掘目标 二.分析方法与过程 客户价值识别最常用的是RFM模型(最近消费时间间隔Recency,消费频率Frequency,消费金额Monetary) 1.EDA(探索性数据分析) #对数据 ...
- C#线程同步(5)- 信号量 Semaphore
文章原始出处 http://xxinside.blogbus.com/logs/47617134.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...
- php登录注册
php 登录注册 注册代码:register.php <style type="text/css"> form{ width:300px; background-col ...
- asp.net 获取网站根目录总结
.CSHttpContext.Current.Server.MapPath();//所在文件夹路径System.Web.HttpContext.Current.Request.PhysicalAppl ...
- python:更改pip源
windows更改pip源 cmd echo %APPDATA% 打开目录 创建文件夹pip 创建pip.ini文件 [global] timeout = 60 index-url = http:// ...