1.content_by_lua_block作用:在http内容处理阶段生成数据
server {
listen 80;
server_name www.a.com;
charset koi8-r;
location = /test {
default_type 'text/plain';
-- content_by_lua_block 执行阶段
content_by_lua_block {
ngx.say("hello world")
}
}
}
2.json解释库 -- cjson
location = /test1 {
content_by_lua_block {
local cjson = require "cjson"
ngx.say(cjson.encode{a=1,b=2,c=3})
}
}
3.HTTP执行阶段
NGX_HTTP_ACCESS_PHASE 在请求前设置对资源的控制
NGX_HTTP_CONTENT_PHASE 处理HTTP请求内容的阶段,一半会和后端服务器进行交互
location /lua_request{
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/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
————————————————
5.nginx的uri匹配优先级
用一个例子来演示会更加清晰