Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140663
  • 博文数量: 12
  • 博客积分: 45
  • 博客等级: 民兵
  • 技术积分: 194
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-03 13:16
文章分类

全部博文(12)

文章存档

2015年(3)

2014年(8)

2013年(1)

分类: 系统运维

2015-04-23 19:13:58

安装注意点
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)记录特定用户的访问延迟,方便统计分析、与故障时定位原因。
  1. if ngx.var.cookie_uin ~= nil then
  2.         local uin = tonumber(string.sub(ngx.var.cookie_uin,2,-1))
  3.         if uin ~= nil and uin >=1000140 and uin<=1000200 then
  4.                 ngx.log(ngx.CRIT, "[Log] uin:".. uin..",rsp_time:"..ngx.var.request_time..",upstream_rsp_time:" .. ngx.var.upstream_response_time .. ",upstream_status:"..ngx
  5. .var.upstream_status..",");
  6.         end
  7. end
2) 实现一个简单的鉴权功能,添加header后传给后端做校验
  1. local resty_md5 = require "resty.core.md5"
  2. local md5 = resty_md5:new()
  3. local client_ip=ngx.req.get_headers()['X-Real-IP']
  4. if client_ip == nil then client_ip = ngx.req.get_headers()['x_forwarded_for'] end
  5. if client_ip == nil then client_ip = ngx.var.remote_addr end
  6. local hex_ip = {}
  7. local ip_str_ascii = {}
  8. string.gsub(client_ip, "%d+", function(item)
  9.    table.insert(hex_ip,string.format("%02x", item))
  10.    table.insert(ip_str_ascii,string.char(item))
  11.    end
  12. )
  13. ipstr_hex=table.concat(hex_ip)
  14. ipstr_ascii=table.concat(ip_str_ascii)
  15. ipstr_salt=string.format("%s%s%s",'QTest^10#Prefix',ipstr_ascii,'Test$Suffix%')

  16. local ok = md5:update(ipstr_salt)
  17. local digest = md5:final()
  18. local str = require "resty.string"
  19. ngx.header["QVia"] = ipstr_hex .. str.to_hex(digest)

3) 实现数据上报接口
   接收post、get数据,解析存入db。而且性能也不错,1000多台设备上报数据,nginx日志里没发现有5xx错误。

  1. local post_args_num=20
  2. local method = ngx.req.get_method()

  3. local gargs = ngx.req.get_uri_args(post_args_num)
  4. if gargs.op ~= "info_report" and gargs.op ~= "proc_report" and gargs.op ~= 'host_report' and gargs.op ~= 'log_report' then
  5.     ngx.say('op type error: '.. gargs.op)
  6.     return false
  7. end

  8. local mysql = require "resty.mysql"
  9. local db, err = mysql:new()
  10. if not db then
  11.     ngx.say("mysql init error ")
  12.     return false
  13. end
  14. db:set_timeout(1000)
  15.     
  16. local ok, err, errno, sqlstate = db:connect{
  17.     host = "127.0.0.1",
  18.     port = 3306,
  19.     database = "test",
  20.     user = "test",
  21.     password = "pass4",
  22.     max_packet_size = 1024 * 1024 }
  23.     
  24. if not ok then
  25.     ngx.say("mysql connect error")
  26.     return false
  27. end

  28. local current_date=os.date("%Y%m%d", os.time())

  29. local args,err = ngx.req.get_post_args(post_args_num)


  30. -- insert log_report
  31. if gargs.op == 'log_report' and ngx.req.get_method() == "POST" then
  32.    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.."\')"
  33.     local res, err, errno, sqlstate = db:query(insert_sql)
  34.     if not res then
  35.         ngx.say("sql: ".. insert_sql .. "\nerrno: " .. errno .. "\nerr:" .. err)
  36.         return false
  37.     end
  38. elseif gargs.op == 'log_report' and ngx.req.get_method() == "GET" then
  39.     if gargs['ip'] ~= nil and gargs['name'] ~= nil then
  40.         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'] .. "\'"
  41.         local res, err, errno, sqlstate = db:query(query_sql)
  42.         if not res[1] then
  43.             --ngx.say("sql: ".. insert_sql .. "\nerrno: " .. errno .. "\nerr:" .. err)
  44.             ngx.say('0')
  45.             return
  46.         else
  47.             ngx.say(res[1].log_size)
  48.         end
  49.     else
  50.         ngx.say("log_report get args error")
  51.         return false
  52.     end
  53. end
4) 自定义规则检查nginx后端upstream健康状态

5) nginx的http请求状态,200,4xx,5xx统计数据实时上报
阅读(3336) | 评论(0) | 转发(0) |
0

上一篇:nginx海量业务运营总结

下一篇:没有了

给主人留下些什么吧!~~