Nginx教程

Nginx与LUA(6)

本文主要是介绍Nginx与LUA(6),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

您好,我是湘王,这是我的慕课手记。值此新春佳节我给您拜年啦祝您在新的一年中所求皆所愿所行皆坦途展宏“兔”有钱“兔”多喜乐常安宁


如前所述,OpenResty是一个基于Nginx与Lua的高性能Web平台,内部集成了大量的Lua库、第三方模块以及大多数依赖模块化既是OpenResty的特色,也是高性能Lua应用的关键

在实际开发中,OpenResty已经为开发者提供了很多成熟且开箱即用的「模块」:cjson、redis客户端、mysql客户端等

lua-resty-redis是为Lua提供的redis客户端,通过它可以完成Redis的操作,默认安装OpenResty时已经自带了该模块可以在github中搜索「lua-resty-redis」找到它

现在就通过LUA来操作Redis

先在虚拟机上安装Redis然后修改Redis的配置文件

注释掉bind 127.0.0.1:

# bind 127.0.0.1

protected-mode设为no:

protected-mode no

注释掉requirepass:

# requirepass 123456

启动redis服务:

systemctl start redis.service

 

创建redis.lua:

vi /usr/local/openresty/nginx/conf/lua/redis.lua

下面是redis.lua的内容

-- 引入redis模块

local redis = require("resty.redis")

-- 创建实例

local red = redis:new()

-- 超时时间

red:set_timeout(1000)

-- 建立连接

local ok, err = red:connect("127.0.0.1", 6379)

if not ok then

    return

end

-- 通过密码访问

-- local res, err = red:auth("123456")

-- if not res then

--     ngx.say("connect to redis error: ", err)

--     return

-- end

-- 保存数据

local res, err = red:set("hello", "hello world")

if not res then

    ngx.say("set hello key error: ", err)

    local ok, err = red:close()

    if not ok then

        ngx.say("close redis error: ", err)

    end

end

-- 获得数据

local resp, err = red:get("hello")

if not resp then

    ngx.say("get hello key error: ", err)

    local ok, err = red:close()

    if not ok then

        ngx.say("close redis error: ", err)

    end

end

-- 判空处理

if resp == ngx.null then

    resp = ''

end

ngx.say("hello: ", resp)

 

修改lua.conf

vi /usr/local/openresty/nginx/conf/lua.conf

其中的内容是

server {

    listen       80;

    server_name  _;

 

    location /redis {

        default_type 'text/html';

        lua_code_cache on;

        content_by_lua_file conf/lua/redis.lua;

    }

}

 

运行测试

重启Nginx服务:

/usr/local/openresty/nginx/sbin/nginx -s reload

执行curl命令:

curl 'http://127.0.0.1/redis'

得到结果:

hello: hello world

 

再次修改修改redis.lua

-- 初始化管道

red:init_pipeline()

red:set("test1", "hello")

red:set("test2", "world")

red:get("test1")

red:get("test2")

local respTable, err = red:commit_pipeline()

-- 判空处理

if respTable == ngx.null then

    respTable = {}

end

-- 结果是按照执行顺序返回的一个table

for i, v in ipairs(respTable) do

    ngx.say("result: ", v)

end

 

运行测试

重启Nginx服务

/usr/local/openresty/nginx/sbin/nginx -s reload

执行curl命令

curl 'http://127.0.0.1/redis'

得到结果

hello: hello world

result : OK

result : OK

result : hello

result : world

 

如同Redis那样,OpenResty也封装好了操作mysql的模块lua-resty-mysql是为Lua提供的mysql客户端,通过它可以完成mysql的操作,默认安装OpenResty时已经自带了该模块可以在github中搜索「lua-resty-mysql」找到它

首先创建创建mysql.lua

vi /usr/local/openresty/nginx/conf/lua/mysql.lua

mysql.lua的内容是

-- 引入mysql模块

local mysql = require("resty.mysql")

-- 创建实例

local db, err = mysql:new()

if not db then

    ngx.say("new  mysql error:", err)

    return

end

-- 超时时间

db:set_timeout(5000)

-- 建立连接

local props = {

    host = "127.0.0.1",

    port = 3306,

    database = "sys",

    user = "root",

    password = "123456"

}

local res, err, errno, state = db:connect(props)

if not res then

    ngx.say("connect to mysql error: ", err, ", errno: ", errno, ", state: ", state)

    return db:close()

end

-- 删除表

local drop_table_sql = "drop table if exists test"

res, err, errno, state = db:query(drop_table_sql)

if not res then

    ngx.say("drop table error: ", err, ", errno: ", errno, ", state: ", state)

    return db:close()

end

-- 创建表

local create_table_sql = "create table test(id int primary key auto_increment, name varchar(100))"

res, err, errno, state = db:query(create_table_sql)

if not res then

    ngx.say("create table error: ", err, ", errno: ", errno, ", state: ", state)

    return db:close()

end

-- 插入

local insert_sql = "insert into test (name) values('xiangwang')"

res, err, errno, state = db:query(insert_sql)

if not res then

    ngx.say("insert error: ", err, ", errno: ", errno, ", state: ", state)

    return db:close()

end

ngx.say("insert rows : ", res.affected_rows, ", id: ", res.insert_id)

-- 更新

local update_sql = "update test set name = 'xiangwang2' where id = " .. res.insert_id

res, err, errno, sqlstate = db:query(update_sql)

if not res then

    ngx.say("update error: ", err, ", errno: ", errno, ", state: ", state)

    return db:close()

end

ngx.say("update rows : ", res.affected_rows)

-- 查询

local select_sql = "select id, ch from test"

res, err, errno, sqlstate = db:query(select_sql)

if not res then

    ngx.say("select error: ", err, ", errno: ", errno, ", state: ", state)

    return db:close()

end

for i, row in ipairs(res) do

    for name, value in pairs(row) do

        ngx.say("select row ", i, " : ", name, " = ", value)

    end

end

-- 防注入

-- local param = ngx.req.get_uri_args()["name"] or ''

-- local query_sql = "select id, name from test where name = " .. ngx.quote_sql_str(param)

-- res, err, errno, state = db:query(query_sql)

-- if not res then

--     ngx.say("select error: ", err, ", errno: ", errno, ", state: ", state)

--     return db:close()

-- end

-- for i, row in ipairs(res) do

--     for name, value in pairs(row) do

--         ngx.say("select row ", i, " : ", name, " = ", value)

--     end

-- end

-- 删除

local delete_sql = "delete from test"

res, err, errno, state = db:query(delete_sql)

if not res then

    ngx.say("delete error: ", err, ", errno: ", errno, ", state: ", state)

    return db:close()

end

ngx.say("delete rows: ", res.affected_rows)

 

修改lua.conf:

vi /usr/local/openresty/nginx/conf/lua.conf

lua.conf的内容是

server {

    listen       80;

    server_name  _;

 

    location /mysql {

        default_type 'text/html';

        lua_code_cache on;

        content_by_lua_file conf/lua/mysql.lua;

    }

}

 

重启Nginx服务:

/usr/local/openresty/nginx/sbin/nginx -s reload

执行curl命令:

curl 'http://127.0.0.1/mysql

得到结果:

insert rows: 1, id: 1

update rows : 1

select row 1 : name = xiangwang2

select row 1 : id = 1

delete rows: 1


 

节日期间您仍然可以随时咨询技术、产品、运营和管理相关问题,请关注后留言。欢迎骚扰,不胜荣幸~再次祝您兔年吉祥万事胜意


这篇关于Nginx与LUA(6)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!