OpenResty(Nginx+Lua)实践

一、OpenResty集成Lua编写简单的HTTP

1.1 测试lua脚本

保持luatest.lua 文件

--用于接收前端数据的对象
local args=nil
--获取前端的请求方式 并获取传递的参数   
local request_method = ngx.var.request_method
--判断是get请求还是post请求并分别拿出相应的数据
if"GET" == request_method then
        args = ngx.req.get_uri_args()
elseif "POST" == request_method then
        ngx.req.read_body()
        args = ngx.req.get_post_args()
        --兼容请求使用post请求,但是传参以get方式传造成的无法获取到数据的bug
        if (args == nil or args.data == null) then
                args = ngx.req.get_uri_args()
        end
end
--打印日志到nginx日志文件,级别INFO/ERR,
ngx.log(ngx.INFO, "url args:", args)
--获取前端传递的name值
local name = args.name
--响应前端
ngx.say("hello:"..name)

1.2 配置nginx访问地址

location /api/luatest{
	default_type text/html;
	//指定当前local访问结果是读取lua 文件
	content_by_lua_file /etc/nginx/conf.d/testlua.lua;
}

1.3 验证结果OpenResty(Nginx+Lua)实践_第1张图片

1.4 content_by_lua

location /api/luatest2{
     content_by_lua '
       if jit then
             ngx.say(jit.version)
       else 
             ngx.say(_VERSION)
       end
    ';
}

二、lua集成redis

参考官网:https://github.com/openresty/redis2-nginx-module

2.1 添加

 # GET /api/redis/set?key=one&val=first%20value  (one = first name)
location /api/redis/set{
   set_unescape_uri $key $arg_key;  # this requires ngx_set_misc
   set_unescape_uri $val $arg_val;  # this requires ngx_set_misc
   redis2_query set $key $val;
   redis2_pass 127.0.0.1:6379;
}

2.2 查询

 # GET /api/redis/set?key=one
location /api/redis/get {
    default_type text/html;
    set_unescape_uri $key $arg_key;  # this requires ngx_set_misc
    redis2_query get $key;
    redis2_pass 127.0.0.1:6379;        
}

OpenResty(Nginx+Lua)实践_第2张图片

# variables can be used below and $ is special
#redis2_raw_query 'get one\r\n';--
redis2_query del key1;
redis2_query lpush key1 C;
# GET /baz?get%20foo%0d%0a
# this requires the ngx_set_misc module
set_unescape_uri $query $query_string; 
redis2_raw_query $query;

2.3 redis集群

upstream redis_cluster {
     server 127.0.0.1:6379;
     server 127.0.0.1:6380;
}

location = /redis {
     redis2_next_upstream error timeout invalid_response;
     redis2_query get foo;
     redis2_pass redis_cluster;
}

2.3 redis + Lua

location /api/redis {
   internal;
   # set_unescape_uri is provided by ngx_set_misc
   set_unescape_uri $query $arg_query;
   redis2_raw_query $query;
   redis2_pass 127.0.0.1:6379;
}

location /api/redis/lua {
   content_by_lua '
      local res = ngx.location.capture("/api/redis",
          { args = { query = "get one\\r\\n" } }
      )
      ngx.print("[" .. res.body .. "]")
   ';
}

Redis Parser

location /api/redis/lua2 {
   content_by_lua_file /etc/nginx/conf.d/redis.lua;
}

location = /api/redis2 {
   internal;
   redis2_raw_queries $args $echo_request_body;
   redis2_pass 127.0.0.1:6379;
}
local parser = require "redis.parser"

local reqs = {
    {"set", "foo", "hello world"},
    {"get", "foo"}
}

local raw_reqs = {}
for i, req in ipairs(reqs) do
    table.insert(raw_reqs, parser.build_query(req))
end

local res = ngx.location.capture("/api/redis?" .. #reqs,
    { body = table.concat(raw_reqs, "") })

if res.status ~= 200 or not res.body then
    ngx.log(ngx.ERR, "failed to query redis")
    ngx.exit(500)
end

local replies = parser.parse_replies(res.body, #reqs)
for i, reply in ipairs(replies) do
    ngx.say(reply[1])
end

根据log可得知build_query 将参数翻译为redis语法

ngx.log(ngx.ERR, "Redis testing request",tostring(parser.build_query(req)) ) 
*3
$3
set
$3
foo
$11
hello world

OpenResty(Nginx+Lua)实践_第3张图片

你可能感兴趣的:(辅助技术)