Nginx 内嵌lua脚本,结合Redis使用
0x00 Nginx 内嵌Lua脚本有下面特点:
-
20k个并发连接
-
Lua脚本能够在Nignx 11个层次的不同层次发挥作用,扩展Ngnix功能
-
Lua速度极快(寄存器指令)
0x01 应用场景
-
在web server端做请求过滤处理(如:WAF、Anti CC等)
0x02 简单配置过程
-
測试环境Ubuntu Server 14.04.2 LTS
-
几个须要下载的模块(注意安装顺序和export路径问题)
-
LuaJIT-2.0.4(A Just-In-Time Compiler for Lua)
-
ngx_devel_kit( Nginx Development Kit)
-
echo-nginx-module( more shell-style goodies to Nginx config file)
-
lua-nginx-module(Embed the Power of Lua into Nginx)
0x03 可能存在的问题。找不到 lua.h 等,是由于luaJIT的lib和inc没有配置在环境变量中
须要这样配置(你实际的本地路径):
export LUAJIT_LIB=/usr/lib/lua
export LUAJIT_INC=/usr/local/include/luajit-2.0
cp /usr/local/include/luajit-<VERSION>/* /usr/local/include/
假设有无法启动的情况。service 能够查看 tail /var/log/syslog 查看错误
假设是nginx无法启动能够查看 tail /var/cache/nginx/error.log
假设已经生成nginx bin文件 能够用 nginx -V 来查看 配置文件是否正确
假设缺少一下模块:
PCRE
sudo apt-get install libpcre3 libpcre3-dev
zlib
sudo apt-get install zlib1g-dev
openssl
sudo apt-get install libssl-dev
ps:特别说明的是。请注意下Nginx的版本号不要下载最新的,可能不支持上面那些模块接口,我用的是Nginx 1.7.4
当中0x02的安装步骤都有安装说明,这里就不细说了
0x04 安装完后
改动nginx.conf文件 (默认路径 /etc/nginx/nginx.conf):
-
加入lua代码
又一次load nginx 配置
sudo /etc/nginx/sbin/nginx -s reload
效果:
2. 加入lua 文件:
加入两个lua_package_path,lua_code_cache(为了不保留lua cache,方便调试。实际项目中须要打开)
总体的lua文件的文件夹(注意lua文件夹中的文件是接下来新建的):
/etc/nginx/lua/hello.lua
/etc/nginx/lua/hello_redis.lua
/etc/nginx/lua/redis.lua
nginx.conf 文件加入:
hello.lua文件内容:
ngx.header.content_type = "text/plain";
ngx.say("say hello from hello.lua");
全部加入的location代码:
然后又一次load nginx 看效果。
3.使用redis(第三条新加的redis):
前提是机器上已有redis-server, Ubuntu上安装是 sudo apt-get install redis-server
hello_redis.lua 内容:
local redis = require "redis"
local cache = redis.new()
local ok, err = cache.connect(cache, '127.0.0.1', '6379')
cache:set_timeout(60000)
if not ok then
ngx.say("failed to connect:", err)
return
end
res, err = cache:set("hello", "redis in nginx_inline_lua")
if not ok then
ngx.say("failed to set hello: ", err)
return
end
ngx.say("set result: ", res)
local res, err = cache:get("hello")
if not res then
ngx.say("failed to get hello: ", err)
return
end
if res == ngx.null then
ngx.say("hello not found.")
return
end
ngx.say("hello: ", res)
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
效果:
0x05 如今为止,简单的一个在Nginx 中内嵌Lua而且操作Redis的过程已经完毕了,在配置时候可能有非常多细小的问题。可是不要放弃,坚持下去。相信你就会成功。
0xFF 附加资料:
http://wiki.nginx.org/HttpLuaModule
http://openresty.org/ (最先完毕Nginx内嵌Lua的Chinese)
转载请注明出处(个人论坛):http://www.byteway.net/thread-index-fid-4-tid-316.htm
相关文章
- 【Redis】yum安装redis
- Redis学习(2)-redis安装
- spring boot单元测试之十:用embedded-redis库做Redis的mock(spring boot 2.4.3)
- redis 的 HyperLogLog
- Redis Cluster 搭建手册
- Redis自旋锁解决分布高并发问题:使线程异步变为同步执行
- NoSQL之Redis(四)--- RedisCluster概念以及搭建
- 理解redis高可用方案
- Atititt java redis jedis 使用 Redis体系 Redis 命令 Redis 命令Redis 键(key)Redis 字符串(String)Redis 哈希(Hash)R
- 【异常】Nginx报错[emerg]: unknown directive “user“ in /usr/local/nginx/conf/nginx.conf:xx报错处理
- 平衡树:为什么Redis内部实现用跳跃表
- windows安装redis
- 【数据库】KeyDB:一款比 Redis 性能强 5 倍的数据库!
- 深入浅出Redis-redis哨兵集群
- Nginx配置文件nginx.conf具体解释
- 005-redis-命令-4、无序集合,5、有序集合
- Python3之redis使用
- 【云原生 • Docker】mysql、tomcat、nginx、redis 环境部署
- CentOS7 下安装 Redis
- Redis---NoSQL数据库介绍
- 【最详细】最新最全Redis面试大全(50道)
- 【redis】Redis缓存失效、雪崩、穿透、击穿、并发等案例分析难题解决方案
- 【redis源码分析】Redis Sentinel 是如何实际解决分布式共识问题的
- Nginx(4):守护进程,一份nginx实现,一份我的实现,看着拿呗
- 配置logstash从redis读取filebeat收集的日志(上)