安装注意点:
lua的介绍与安装,已有不错的文章,这里只备注几个需要注意的地方:
1. 调试
nginx编译时最好打开-with-debug标签,并配置error_log的级别为debug。方便调试与定位问题。
2. lua_package_path
如lua_package_path "/usr/local/nginx/addon/lualib/?.lua;;";
是指lua代码中require的相对路径为:/usr/local/nginx/addon/lualib/
lua语法:
看完这里,写些简单的应用已经足够。
实际应用:
1)记录特定用户的访问延迟,方便统计分析、与故障时定位原因。
-
if ngx.var.cookie_uin ~= nil then
-
local uin = tonumber(string.sub(ngx.var.cookie_uin,2,-1))
-
if uin ~= nil and uin >=1000140 and uin<=1000200 then
-
ngx.log(ngx.CRIT, "[Log] uin:".. uin..",rsp_time:"..ngx.var.request_time..",upstream_rsp_time:" .. ngx.var.upstream_response_time .. ",upstream_status:"..ngx
-
.var.upstream_status..",");
-
end
-
end
2) 实现一个简单的鉴权功能,添加header后传给后端做校验
-
local resty_md5 = require "resty.core.md5"
-
local md5 = resty_md5:new()
-
local client_ip=ngx.req.get_headers()['X-Real-IP']
-
if client_ip == nil then client_ip = ngx.req.get_headers()['x_forwarded_for'] end
-
if client_ip == nil then client_ip = ngx.var.remote_addr end
-
local hex_ip = {}
-
local ip_str_ascii = {}
-
string.gsub(client_ip, "%d+", function(item)
-
table.insert(hex_ip,string.format("%02x", item))
-
table.insert(ip_str_ascii,string.char(item))
-
end
-
)
-
ipstr_hex=table.concat(hex_ip)
-
ipstr_ascii=table.concat(ip_str_ascii)
-
ipstr_salt=string.format("%s%s%s",'QTest^10#Prefix',ipstr_ascii,'Test$Suffix%')
-
-
local ok = md5:update(ipstr_salt)
-
local digest = md5:final()
-
local str = require "resty.string"
-
ngx.header["QVia"] = ipstr_hex .. str.to_hex(digest)
3) 实现数据上报接口
接收post、get数据,解析存入db。而且性能也不错,1000多台设备上报数据,nginx日志里没发现有5xx错误。
-
local post_args_num=20
-
local method = ngx.req.get_method()
-
-
local gargs = ngx.req.get_uri_args(post_args_num)
-
if gargs.op ~= "info_report" and gargs.op ~= "proc_report" and gargs.op ~= 'host_report' and gargs.op ~= 'log_report' then
-
ngx.say('op type error: '.. gargs.op)
-
return false
-
end
-
-
local mysql = require "resty.mysql"
-
local db, err = mysql:new()
-
if not db then
-
ngx.say("mysql init error ")
-
return false
-
end
-
db:set_timeout(1000)
-
-
local ok, err, errno, sqlstate = db:connect{
-
host = "127.0.0.1",
-
port = 3306,
-
database = "test",
-
user = "test",
-
password = "pass4",
-
max_packet_size = 1024 * 1024 }
-
-
if not ok then
-
ngx.say("mysql connect error")
-
return false
-
end
-
-
local current_date=os.date("%Y%m%d", os.time())
-
-
local args,err = ngx.req.get_post_args(post_args_num)
-
-
-
-- insert log_report
-
if gargs.op == 'log_report' and ngx.req.get_method() == "POST" then
-
local insert_sql="replace into t_instance_log (date,ip,l1_business,l2_business,l3_business,module,name,pname,author,log_size,last_update)" .. " values (\'".. args.date .."\',\'".. args.ip .."\',\'".. args.l1_business.."\',\'".. args.l2_business.."\',\'".. args.l3_business .."\',\'".. args.modules .."\',\'".. args.name .."\',\'"..args.pkg_name.."\',\'".. args.author.."\',"..args.log_size..",\'"..args.last_update.."\')"
-
local res, err, errno, sqlstate = db:query(insert_sql)
-
if not res then
-
ngx.say("sql: ".. insert_sql .. "\nerrno: " .. errno .. "\nerr:" .. err)
-
return false
-
end
-
elseif gargs.op == 'log_report' and ngx.req.get_method() == "GET" then
-
if gargs['ip'] ~= nil and gargs['name'] ~= nil then
-
local query_sql="select round(log_size,2) as log_size from t_instance_log where date = ".. current_date .." and ip = \'".. gargs['ip'] .. "\' and name = \'"..gargs['name'] .. "\'"
-
local res, err, errno, sqlstate = db:query(query_sql)
-
if not res[1] then
-
--ngx.say("sql: ".. insert_sql .. "\nerrno: " .. errno .. "\nerr:" .. err)
-
ngx.say('0')
-
return
-
else
-
ngx.say(res[1].log_size)
-
end
-
else
-
ngx.say("log_report get args error")
-
return false
-
end
-
end
4) 自定义规则检查nginx后端upstream健康状态
5) nginx的http请求状态,200,4xx,5xx统计数据实时上报
阅读(3380) | 评论(0) | 转发(0) |