openresty lua 使用

简单例子

打印请求头

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
listen 8000;
client_max_body_size 512m;
access_log /data/logs/extsvc.log nginxlog_extsvc_json;
set $req_header "";
header_filter_by_lua '
local h = ngx.req.get_headers()
for k, v in pairs(h) do
ngx.var.req_header=ngx.var.req_header..k..": "..v.."; "
end
';

location / {
proxy_pass http://service:8080/;
# 配置缓存大小
proxy_buffers 256 4k;
# 关闭磁盘缓存读写减少i/o
proxy_max_temp_file_size 0;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 1200;
proxy_send_timeout 1200;
proxy_read_timeout 1200;
}
}

连接mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
--1) 定义数据库连接属性
local props = {
host = "xxxx",
port = 3306,
database = "xxx",
user = "xxx",
password = "xxxx",
charset = 'utf8mb4',
max_packet_size = 1024 * 1024
}
-- 2) 自定义close函数
local function close_db(db)
if not db then
return
end

-- 连接池机制:不调用close,选择keepalive 确保性能
local pool_max_idle_time = 10000 --毫秒
local pool_size = 100 --连接池大小
local ok, err = db:set_keepalive(pool_max_idle_time, pool_size)
if not ok then
-- ngx.say("set keepalive error : ", err)
ngx.log(ngx.ERR, "set keepalive error : ", err)
end
end
-- 3) 引入mysql实例
local mysql = require("resty.mysql")
-- 4) 创建实例
local db, err = mysql:new()
if not db then
-- ngx.say("new mysql error : ", err)
ngx.log(ngx.ERR, "new mysql error : ", err)
return
end

--4) 设置超时时间
db:set_timeout(10000)

-- 6) 建立连接
local res,err,errno,sqlstate = db:connect(props)
-- 备注:异常判断,异常消息统一处理
if not res then
ngx.log(ngx.ERR, "connect to mysql error:", err,", errno:", errno,", sqlstate:", sqlstate)
return close_db(db)
end
ngx.say("connectd")
-- 获取access_token
ngx.say(ngx.var.arg_access_token)
ngx.log(ngx.INFO, ngx.var.arg_access_token)
-- 7) sql 语句操作 -->过程省略

local select_sql = "show PROCESSLIST"
res, err, errno, sqlstate = db:query(select_sql)
if not res then
ngx.say("select table error :", err,", errno", errno, ", sqlstate : ",sqlstate)
return close_db(db)
end
ngx.say(sqlstate)

-- 多条'结果集'的处理
for i, row in ipairs(res) do
-- ngx.say(row.username, row.phone)
for key,value in pairs(row) do
ngx.say("select row", i," : ", key, " = ", value)
end
ngx.say("<br/>===================")
end

-- 8) 关闭连接 -->由于使用连接池,不需要每次都使用密码
close_db(db)

修改响应体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
location / {
root html;
index index.html index.htm;
default_type text/plain;
set $qw_corp_id "";
#content_by_lua_file /usr/local/openresty/nginx/lua/init.lua;
rewrite_by_lua_file /usr/local/openresty/nginx/lua/init.lua;
#body_filter_by_lua '
# local str = ngx.arg[1]
# str = string.gsub(str, "CHANGE_URL", ngx.var.host)
# ngx.arg[1] = str
# ';
#sub_filter_types *;
#sub_filter_once off;
#sub_filter 'CHANGE_URL' $host;
#proxy_set_header X-My-Header $qw_corp_id;
#proxy_pass http://xxxx;

}

打印响应体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
server {
listen 800;
location / {
default_type text/html;
lua_need_request_body on;
set $resp_body "";
body_filter_by_lua_block {
local cjson = require("cjson")
local resp_body = ngx.arg[1]
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
ngx.log(ngx.ERR, ngx.var.resp_body)
if ngx.var.resp_body == "ret_body" then
ngx.log(ngx.ERR,"判断正确\n")
else
ngx.log(ngx.ERR,"其他内容\n")
end

local success, response_json = pcall(cjson.decode, ngx.var.resp_body)
if success then
-- 打印JSON内容
local response_json_str = cjson.encode(response_json)
ngx.log(ngx.ERR, "打印JSON内容" .. response_json_str .. "\n")
else
-- 打印原始响应内容
ngx.log(ngx.ERR, "打印原始响应内容:" .. ngx.var.resp_body .. "\n")
end

end
}
proxy_pass http://127.0.0.1:801;
}
}