Nginx内嵌lua脚本,结合Redis使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Nginx内嵌lua脚本,结合Redis使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3420字,纯文字阅读大概需要5分钟。
内容图文
![Nginx内嵌lua脚本,结合Redis使用](/upload/InfoBanner/zyjiaocheng/155/b62fe61253624e09bc25ac5a92df11a2.jpg)
20k个并发连接
Lua脚本可以在Nignx 11个层次的不同层次发挥作用,扩展Ngnix功能
Lua速度极快(寄存器指令)
0x01 应用场景
在web server端做请求过滤处理(如:WAF、Anti CC等)
0x02 简单配置过程
测试环境Ubuntu Server 14.04.2 LTS
几个需要下载的模块(注意安装顺序和export路径问题)
Nginx 1.7.4
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-
如果有无法启动的情况,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://tengine.taobao.org/
转载请注明出处(个人论坛):http://www.byteway.net/thread-index-fid-4-tid-316.htm
以上就介绍了Nginx 内嵌lua脚本,结合Redis使用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
内容总结
以上是互联网集市为您收集整理的Nginx内嵌lua脚本,结合Redis使用全部内容,希望文章能够帮你解决Nginx内嵌lua脚本,结合Redis使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。