1、前言
Lua和OpenResty安装请参考该链接:https://blog.csdn.net/pkxwyf/article/details/105360507
2、示例1-输出数据到页面
2.1、在root/lua目录下创建hello.lua
ngx.header.content_type="application/json;charset=utf‐8";
ngx.say("{flag:true,code:20000}");
- 第1句表示指定输出的内容类型为json,字符集为utf-8
- 第2句为输出语句
2.2、在Nginx的配置文件nginx.conf 中的server节点下添加配置
进入/usr/local/openresty/nginx/conf目录,编辑nginx.conf文件:
location /hello {
content_by_lua_file /root/lua/hello.lua
}
2.3、重新启动nginx让配置生效
./nginx ‐s reload
2.4、浏览器输入 http://192.168.33.133/hello 查看测试结果:
3、示例2-JSON转换并输出
3.1、在root/lua目录下创建myJson.lua
ngx.header.content_type="application/json;charset=utf‐8";
local cjson =require("cjson");
local result = {
id = '9527',
name= 'jack'
}
ngx.say(cjson.encode(result));
3.2、在nginx的配置文件nginx.conf 中的server节点下添加配置
location /myJson {
content_by_lua_file /root/lua/myJson.lua
}
3.3、重新启动nginx 浏览器测试:http://192.168.33.133/myJson
4、示例3-接收请求参数
4.1、在root/lua目录下创建args.lua
ngx.header.content_type="application/json;charset=utf-8";
--获取页面传递的所有参数
local args= ngx.req.get_uri_args();
local id=args["id"];
--打印接收过来的参数
ngx.say(id);
- ngx.req.get_uri_args()方法用于获取页面参数
4.2、在nginx的配置文件nginx.conf 中的server节点下添加配置
location /args {
content_by_lua_file /root/lua/args.lua;
}
4.3、重新启动nginx 浏览器测试 http://192.168.33.133/args?id=200 ,页面显示200。如果不传参数页面显示nil
5、示例4-查询mysql数据库
5.1、在root/lua目录下创建mysql.lua
ngx.header.content_type="application/json;charset=utf-8";
--引入cjson模块
local cjson =require("cjson");
--引入mysql模块
local mysql=require("resty.mysql");
--实例化获得数据库对象
local db = mysql:new();
local props ={
host = "192.168.33.133",
port = 3306,
database = "数据库名",
user ="root",
password ="root"
}
-- 连接数据库
db:connect(props);
--查询广告表
local select_sql="select image,url from tb_content";
--查询获得结果集
local res = db:query(select_sql);
--关闭数据库连接
db:close();
-- 输出浏览器
ngx.say(cjson.encode(res));
5.2、在nginx的配置文件nginx.conf 中的server节点下添加配置
location /mysql {
content_by_lua_file /root/lua/mysql.lua;
}
5.3、重新启动nginx 浏览器测试 http://192.168.33.133/mysql
6、示例5-操作redis数据库
6.1、往redis中存值
1、在root/lua目录下创建redis_write.lua
ngx.header.content_type="application/json;charset=utf-8";
--引入redis模块
local redis= require("resty.redis");
--创建redis对象
local red = redis:new();
--连接数据库
red:connect("192.168.33.133",6379 );
--向redis存值
red:set("username","rose");
--关闭连接
red:close();
2、在nginx的配置文件nginx.conf 中的server节点下添加配置
location /redis_write {
content_by_lua_file /root/lua/redis_write.lua;
}
3、重新启动nginx 浏览器测试 http://192.168.33.133/redis_write
6.2、从redis中获取值
1、在root/lua目录下创建redis_read.lua
ngx.header.content_type="application/json;charset=utf-8";
--引入redis模块
local redis= require("resty.redis");
--实例化redis对象
local red = redis:new();
--连接数据库
red:connect("192.168.33.133",6379);
--从redis取值
local name= red:get("testname");
--关闭连接
red:close();
ngx.say(name);
2、在nginx的配置文件nginx.conf 中的server节点下添加配置
location /redis_read {
content_by_lua_file /root/lua/redis_read.lua;
}
3、重新启动nginx 浏览器测试http://192.168.33.133/redis_read
7、示例6-Nginx本地缓存
7.1、准备工作
- 开启本地缓存:修改nginx配置文件 vi /usr/local/openresty/nginx/conf/nginx.conf 在 http节点下添加配置:
lua_shared_dict dis_cache 128m; # 开启nginx缓存模块,缓存空间128m
7.2、向nginx本地缓存存值
ngx.header.content_type="application/json;charset=utf-8";
local cache_ngx= ngx.shared.dis_cache;
--第三个参数是过期时间(秒),不写第三个参数表示不过期
cache_ngx:set("info","rose",20);
7.3、从nginx本地缓存取值
ngx.header.content_type="application/json;charset=utf-8";
local cache_ngx= ngx.shared.dis_cache;
local info= cache_ngx:get('info');
ngx.say(info);
7.4、删除nginx本地缓存
ngx.header.content_type="application/json;charset=utf-8";
local args = ngx.req.get_uri_args();
local id=args["id"];
local cache_ngx = ngx.shared.dis_cache;
local info = cache_ngx:delete('content_cache_'..id);
ngx.say(info);