lua nginx 使用 redis mysql模块

worker_processes  1;
events {
    
    
    worker_connections  1024;
}
http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    lua_package_path "/usr/local/openresty2/nginx/lualib/?.lua;;"; #lua 模块
    lua_package_cpath "/usr/local/openresty2/nginx/lualib/?.so;;"; #c 模块
    #gzip  on;
server {
    
    
    listen 80 ;
    location / {
    
    
    root  /usr/local/openresty2/nginx/nginx/html;
    index  index.html;
  }
    location /lua {
    
    
     default_type text/html;
    lua_code_cache off;
    content_by_lua_file lua/hello.lua;

}
location /redis_test{
    
    
     default_type text/html;
        lua_code_cache off;
            content_by_lua_file lua/redis_test.lua;
        }
location /mysql_test {
    
    
           default_type text/html;
        lua_code_cache off;
            content_by_lua_file lua/mysql_test.lua;
        }
    location /capture {
    
    
           default_type text/html;
        lua_code_cache off;
            content_by_lua_file lua/capture.lua;
                    }
}
}

#cat mysql_test.lua
local request_args_tab = ngx.req.get_uri_args()
for k, v in pairs(request_args_tab) do
    test = k
end
local mysql = require "resty.mysql"
local db, err = mysql:new()

if not db then
        ngx.say("failed to instantiate mysql: ", err)
        return
end

db:set_timeout(1000)

local ok, err, errno, sqlstate = db:connect{
    
    
        host = "127.0.0.1",
        port = 3306,
        database = "ngx_lua",
        user = "root",
        password="123456",
        max_packet_size = 1024 * 1024
}

if not ok then
        ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
        return
end

ngx.say("connected to mysql.")

--local res, err, errno, sqlstate = db:query("drop table if exists cats")
--if not res then
--        ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
--        return
--end
--
--res, err, errno, sqlstate = db:query("create table cats " .. "(id int not null primary key auto_increment, "
--                                        .. "name varchar(30))")
--if not res then
--        ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
--        return
--end
--
--ngx.say("table cats created.")
--
--res, err, errno, sqlstate = db:query("insert into cats(name) " .. "values (\'Bob\'),(\'\'),(null)")
--if not res then
--        ngx.say("bad request: ", err, ": ", errno, ": ", sqlstate, ".")
--        return
--end

--ngx.say(res.affected_rows, " rows inserted into table cats ", "(last insert id: ", res.insert_id, ")")

--res, err, errno, sqlstate = db:query("select * from ngx_lua.cats order by id asc", 10)
okst = [[select * from ngx_lua.cats where name="]]..test..[["]]
ngx.say(okst)
res, err, errno, sqlstate = db:query(okst)
if not res then
        ngx.say("bad result ", err, ": ", errno, ": ", sqlstate, ".")
        return
end

local cjson = require "cjson"
ngx.say("result: ", cjson.encode(res))

local ok, err = db:set_keepalive(1000, 100)
if not ok then
        ngx.say("failed to set keepalive: ", err)
        return
end
#cat redis_test.lua
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
        ngx.say("failed to connect: ", err)
        return
end

ngx.say("set result: ", ok)

local res, err = red:get("dog")
if not res then
        ngx.say("failed to get doy: ", err)
        return
end

if res == ngx.null then
        ngx.say("dog not found.")
        return
end

ngx.say("dog: ", res)

#cat capture.lua
local res1,res2,res3 = ngx.location.capture_multi{
    
    
        {
    
    "/mysql_test", {
    
    args="Bob=1&id=1"}},
        {
    
    "/redis_test", {
    
    args="t=2&id=2"}},
        {
    
    "/lua", {
    
    args="t=3&id=3"}},
}

ngx.header.content_type="text/plain"
ngx.say(res1.body)
ngx.say(res2.body)
ngx.say(res3.body)
#cat hello.lua
ngx.say('hello ngx_lua!!!!');

注意使用ngx.location.capture_multi必须要打开lua_code_cache on
#cat capture.lua
local res1,res2 = ngx.location.capture_multi{
    
    
        {
    
    "/mysql_test?Bob=123123"},
        {
    
    "/redis_test"},
   --     {
    
    "/lua"},
}

ngx.header.content_type="text/plain"
ngx.say(res1.body)
pcall(print,res1)
--ngx.say(res2.body)
--ngx.say(res3.body)

猜你喜欢

转载自blog.csdn.net/weixin_43606975/article/details/128702398