Nginx Lua API
来自Linux78|wiki
和一般的Web Server类似,我们需要接收请求、处理并输出响应。而对于请求我们需要获取如请求参数、请求头、Body体等信息;而对于处理就是调用相应的Lua代码即可;输出响应需要进行响应状态码、响应头和响应内容体的输出。因此我们从如上几个点出发即可。
目录
接收请求
openResty.conf配置文件
server {
listen 80;
server_name _;
location ~ /lua_request/(\d+)/(\d+) {
# 设置nginx变量
set $a $1;
set $b $host;
default_type 'text/html';
lua_code_cache off;
# nginx内容处理
# content_by_lua_file /usr/openResty/lua/test.lua;
content_by_lua_file /usr/example/lua/test_request.lua;
# 内容体处理完成后调用
echo_after_body "ngx.var.b $b";
}
}
标题文字
test_request.lua
-- nginx变量
local var = ngx.var
ngx.say("ngx.var.a : ", var.a, "
")
ngx.say("ngx.var.b : ", var.b, "
")
ngx.say("ngx.var[2] : ", var[2], "
")
ngx.var.b = 2;
ngx.say("
")
-- 请求头
local headers = ngx.req.get_headers()
ngx.say("headers begin", "
")
ngx.say("Host : ", headers["Host"], "
")
ngx.say("user-agent : ", headers["user-agent"], "
")
ngx.say("user-agent : ", headers.user_agent, "
")
for k, v in pairs(headers) do
if type(v) == "table" then
ngx.say(k, ":", table.concat(v, ","), "
")
else
ngx.say(k, " : ", v, "
")
end
end
ngx.say("headers end", "
")
ngx.say("
")
-- get请求uri参数
ngx.say("uri args begin", "
")
local uri_args = ngx.req.get_uri_args()
for k, v in pairs(uri_args) do
if type(v) == "table" then
ngx.say(k, " : ", table.concat(v, ", "), "
")
else
ngx.say(k, ": ", v, "
")
end
end
ngx.say("uri args end", "
")
ngx.say("
")
-- post请求参数
ngx.req.read_body()
ngx.say("post args begin", "
")
local post_args = ngx.req.get_post_args()
for k, v in pairs(post_args) do
if type(v) == "table" then
ngx.say(k, ":", table.concat(v, ", "), "
")
else
ngx.say(k, ": ", v, "
")
end
end
ngx.say("post args end", "
")
ngx.say("
")
-- 请求的http协议版本
ngx.say("ngx.req.http_version:", ngx.req.http_version(), "
")
-- 请求方法
ngx.say("ngx.req.get_method:", ngx.req.get_method(), "
")
-- 原始的请求头内容
ngx.say("ngx.req.raw_header:", ngx.req.raw_header(), "
")
-- 请求的body内容体
ngx.say("ngx.req.get_body_data():", ngx.req.get_body_data(), "
")
ngx.say("
")
ngx.var : nginx变量,如果要赋值如ngx.var.b = 2,此变量必须提前声明;另外对于nginx location中使用正则捕获的捕获组可以使用ngx.var[捕获组数字]获取;
ngx.req.get_headers:获取请求头,默认只获取前100,如果想要获取所以可以调用ngx.req.get_headers(0);获取带中划线的请求头时请使用如headers.user_agent这种方式;如果一 个请求头有多个值,则返回的是table;
ngx.req.get_uri_args:获取url请求参数,其用法和get_headers类似;
ngx.req.get_post_args:获取post请求内容体,其用法和get_headers类似,但是必须提前调用ngx.req.read_body():来读取body体(也可以选择在nginx配置文件使用
lua_need_request_body on;开启读取body体,但是官方不推荐);
ngx.req.raw_header:未解析的请求头字符串;
ngx.req.get_body_data:为解析的请求body体内容字符串。
如上方法处理一般的请求基本够用了。另外在读取post内容体时根据实际情况设置client_body_buffer_size和client_max_body_size来保证内容在内存而不是在文件中。
使用如下脚本测试
wget --post-data 'a=1&b=2' 'http://127.0.0.1/lua_request/1/2?a=3&b=4' -O -
结果如下所示:
wget --post-data 'a=1&b=2' 'http://127.0.0.1/lua_request/1/2?a=3&b=4' -O - --2019-11-25 10:01:01-- http://127.0.0.1/lua_request/1/2?a=3&b=4 Connecting to 127.0.0.1:80... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [text/html] Saving to: ‘STDOUT’ [<=> ] 0 --.-K/s ngx.var.a : 1
ngx.var.b : 127.0.0.1
ngx.var[2] : 2
headers begin
Host : 127.0.0.1
user-agent : Wget/1.14 (linux-gnu)
user-agent : Wget/1.14 (linux-gnu)
host : 127.0.0.1
content-type : application/x-www-form-urlencoded
user-agent : Wget/1.14 (linux-gnu)
accept : */*
content-length : 7
connection : Keep-Alive
headers end
uri args begin
a: 3
b: 4
uri args end
post args begin
b: 2
a: 1
post args end
ngx.req.http_version:1.1
ngx.req.get_method:POST
ngx.req.raw_header:POST /lua_request/1/2?a=3&b=4 HTTP/1.1 User-Agent: Wget/1.14 (linux-gnu) Accept: */* Host: 127.0.0.1 Connection: Keep-Alive Content-Type: application/x-www-form-urlencoded Content-Length: 7
ngx.req.get_body_data():a=1&b=2
ngx.var.b 2 [ <=> ] 871 --.-K/s in 0s 2019-11-25 10:01:01 (1.78 MB/s) - written to stdout [871]
输出响应
openResty.conf配置文件增加配置
location /lua_response_1 {
default_type "text/html";
content_by_lua_file /usr/openResty/lua/test_response_1.lua;
}
test_response_1.lua
-- 写响应头
ngx.header.a = "1"
-- 多个响应头可用table
ngx.header.b = {"2", "3"}
-- 输出响应
ngx.say("a", "b", "
")
ngx.print("c", "d", "
")
-- 200状态码退出
return ngx.exit(200)
ngx.header:输出响应头;
ngx.print:输出响应内容体;
ngx.say:通ngx.print,但是会最后输出一个换行符;
ngx.exit:指定状态码退出。
使用postman测试结果如下
重定向
openResty.conf配置文件增加配置
location /lua_response_2 {
default_type "text/html";
content_by_lua_file /usr/openResty/lua/test_response_2.lua;
}
test_response_2.lua
ngx.redirect("https://www.baidu.com", 302)
ngx.redirect:重定向
ngx.status:状态码,设置响应的状态码
ngx.resp.get_headers():获取设置的响应状态码
ngx.send_headers():发送响应状态码,当调用ngx.say/ngx.print时自动发送响应状态码;可以通过
ngx.headers_sent=true:判断是否发送了响应状态码
其他API
openResty.conf配置文件增加配置
location /lua_other {
default_type "text/html";
content_by_lua_file /usr/openResty/lua/test_other.lua;
}
test_other.lua
-- 未经解码的请求uri
local request_uri = ngx.var.request_uri;
ngx.say("request_uri : ", request_uri, "
");
-- 解码
ngx.say("decode request_uri : ", ngx.unescape_uri(request_uri), "
");
-- MD5
ngx.say("ngx.md5 : ", ngx.md5("123"), "
");
-- http time
ngx.say("ngx.http_time : ", ngx.http_time(ngx.time()), "
");
ngx.escape_uri/ngx.unescape_uri : uri编码解码;
ngx.encode_args/ngx.decode_args:参数编码解码;
ngx.encode_base64/ngx.decode_base64:BASE64编码解码;
ngx.re.match:nginx正则表达式匹配;
更多Nginx Lua API请参考 http://wiki.nginx.org/HttpLuaModule#Nginx_API_for_Lua