参考:Openresty最佳案例 | 第4篇:OpenResty常见的api
以下样例的参数说明参考上面的博客
1.配置lua项目化
cd /usr/local/openresty
mkdir example
cp -r lualib/ /example/
mkdir example/lua
cd example
vim example.conf
server {
listen 8083;
server_name _;
location /lua {
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/local/openresty/example/lua/test.lua;
}
}
vim lua/test.lua
ngx.say("hello world")
vim /usr/local/openresty/nginx/conf/nginx.conf
#在http 块中添加
# 添加include example.conf包含此文件片段
include /usr/local/openresty/example/example.conf;
配置路线是:在nginx.conf的http块中,添加 include /usr/local/openresty/example/example.conf;
而example.conf中添加 content_by_lua_file /usr/local/openresty/example/lua/test.lua;
浏览器访问 http://192.168.1.7:8083/lua ---跳转到上面的test.lua来处理
2.获取请求参数
vim /usr/local/openresty/example/example.conf
# 增加一个 location /lua_var
server {
listen 8083;
server_name _;
location /lua {
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/local/openresty/example/lua/test.lua;
}
location /lua_var {
default_type 'text/plain';
content_by_lua_block {
ngx.say(ngx.var.arg_a)
}
}
}
http://192.168.1.7:8083/lua_var?a=aganlian
返回:aganlian
3.获取请求类型
vim /usr/local/openresty/example/example.conf
# 增加一个 location
location /lua_request {
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/local/openresty/example/lua/lua_request.lua;
}
vim /usr/local/openresty/example/lua/lua_request.lua
local arg = ngx.req.get_uri_args()
for k,v in pairs(arg) do
ngx.say("[GET ] key:", k, " v:", v)
end
ngx.req.read_body() -- 解析 body 参数之前一定要先读取 body
local arg = ngx.req.get_post_args()
for k,v in pairs(arg) do
ngx.say("[POST] key:", k, " v:", v)
end
curl 'http://192.168.1.7:8083/lua_request?a=323&b=ss' -d 'c=12w&d=2se3'
4.获取请求头
vim /usr/local/openresty/example/lua/lua_request.lua
# 添加新内容
local headers = ngx.req.get_headers()
ngx.say("headers begin", "<br/>")
ngx.say("Host : ", headers["Host"], "<br/>")
ngx.say("user-agent : ", headers["user-agent"], "<br/>")
ngx.say("user-agent : ", headers.user_agent, "<br/>")
for k,v in pairs(headers) do
if type(v) == "table" then
ngx.say(k, " : ", table.concat(v, ","), "<br/>")
else
ngx.say(k, " : ", v, "<br/>")
end
end
curl 'http://192.168.1.7:8083/lua_request?a=323&b=ss' -d 'c=12w&d=2se3'
5.获取http的其他内容
vim /usr/local/openresty/example/lua/lua_request.lua
# 添加新内容
ngx.say("ngx.req.http_version : ", ngx.req.http_version(), "<br/>")
--请求方法
ngx.say("ngx.req.get_method : ", ngx.req.get_method(), "<br/>")
--原始的请求头内容
ngx.say("ngx.req.raw_header : ", ngx.req.raw_header(), "<br/>")
--请求的body内容体
ngx.say("ngx.req.get_body_data() : ", ngx.req.get_body_data(), "<br/>")
ngx.say("<br/>")
curl 'http://192.168.1.7:8083/lua_request?a=323&b=ss' -d 'c=12w&d=2se3'
6.输出响应
vim exmaple.conf
# 添加新内容
location /lua_response{
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/local/openresty/example/lua/lua_response.lua ;
}
vim /usr/local/openresty/example/lua/lua_response.lua
ngx.header.a="1"
ngx.header.b={"a","b"}
ngx.say("hello","</br>")
ngx.print("sss")
return ngx.exit(200)
curl 'http://192.168.1.7:8083/lua_response'
7.输出日志
vim exmaple.conf
# 添加新内容
location /lua_log{
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/local/openresty/example/lua/lua_log.lua;
}
vim /usr/local/openresty/example/lua/lua_log.lua
local log="i'm log"
local num =10
ngx.log(ngx.ERR, "log",log)
ngx.log(ngx.INFO,"num:" ,num)
curl 'http://192.168.1.7:8083/lua_log'
tail -fn 1000 /usr/local/openresty/nginx/logs/error.log
8.内部调用
vim exmaple.conf
# 添加新内容
location /lua_sum{
# 只允许内部调用
internal;
# 这里做了一个求和运算只是一个例子,可以在这里完成一些数据库、
# 缓存服务器的操作,达到基础模块和业务逻辑分离目的
content_by_lua_block {
local args = ngx.req.get_uri_args()
ngx.say(tonumber(args.a) + tonumber(args.b))
}
}
location = /lua_sum_test {
content_by_lua_block {
local res = ngx.location.capture("/lua_sum", {args={a=3, b=8}})
ngx.say("status:", res.status, " response:", res.body)
}
}
curl 'http://192.168.1.7:8083/lua_sum_test'
9.重定向
vim exmaple.conf
# 添加新内容
location /lua_redirect {
default_type 'text/html';
content_by_lua_file /usr/local/openresty/example/lua/lua_redirect.lua;
}
vim /usr/local/openresty/example/lua/lua_redirect.lua
ngx.redirect("http://www.aganliang.com",302)
10.共享内存
vim nginx.conf
# http块中添加
lua_shared_dict shared_data 1m;
vim exmaple.conf
# 添加新内容
location /lua_shared_dict{
default_type 'text/html';
content_by_lua_file /usr/local/openresty/example/lua/lua_shared_dict.lua;
}
vim /usr/local/openresty/example/lua/lua_shared_dict.lua
local shared_data = ngx.shared.shared_data
local i = shared_data:get("i")
if not i then
i = 1
shared_data:set("i",i)
end
i = shared_data:incr("i",1)
ngx.say("i:",i)