openresty ngx_lua请求响应,
openresty ngx_lua请求响应,
openresty ngx_lua请求响应
请求头操作
ngx.req.set_header:添加、修改请求头
语法格式:ngx.req.set_header(name, value) * name 如果不存在,表示添加 * name 如果存在,表示修改 使用环境:set_by_lua*、rewrite_by_lua* access_by_lua*、content_by_lua* header_filter_by_lua*、body_filter_by_lua* # 示例 ngx.req.set_header(name, "gtlx"):设置单个值 ngx.req.set_header(test, {"1","2"}):使用数组设置多个值
ngx.req.clear_header:删除请求头
语法格式:ngx.req.clear_header(name) 使用环境:set_by_lua*、rewrite_by_lua* access_by_lua*、content_by_lua* header_filter_by_lua*、body_filter_by_lua* # 示例 ngx.req.clear_header(name):直接删除请求头 ngx.req.set_header(test, nil):通过设置为nil,删除请求头
ngx.req.get_headers:获取请求头
语法格式:ngx.req.get_headers(max_headers?, raw?) 使用环境:set_by_lua*、rewrite_by_lua* access_by_lua*、content_by_lua* header_filter_by_lua*、body_filter_by_lua* # 示例 location / { content_by_lua_block { local ngx = require "ngx"; local headers = ngx.req.get_headers(); for key,value in pairs(headers) do print(key .. value) end ngx.say(h["name"]) #获取名为name的请求头 #等同于:ngx.var.http_name } }
请求体操作
lua_need_request_body:强制获取请求体,默认不读取
语法格式:lua_need_request_body on|off * off(默认):表示不读取请求体数据,通过ngx.var.request_body读取的数据为空 * on:开启读取请求体数据,ngx_lua不推荐使用这种方式读取请求体 注意事项 * 如果读取的请求体数据超过client_body_buffer_size大小, $request_body、ngx.var.request_body读取的数据为空 * client_body_buffer_size、client_max_body_size设置相同,避免出现这种情况
ngx.req.read_body:开启读取请求体
语法:ngx.req.read_body() 环境:rewrite_by_lua*、access_by_lua*、content_by_lua* * 同步读取请求体,且不会阻塞nginx事件循环
ngx.req.get_body_file:从临时文件读取请求体数据
语法:file_name = ngx.req.get_body_file() 环境:rewrite_by_lua*、access_by_lua*、content_by_lua* * 获取存放请求数据的临时文件,并读取数据,以字符串返回 * 尽量控制请求体的大小,避免使用临时文件
ngx.req.get_body_data:从内存中读取请求体,返回字符串
语法:data = ngx.req.get_body_data() 环境:rewrite_by_lua*、access_by_lua*、content_by_lua*、log_by_lua* * 从内存中读取请求体,返回字符串
ngx.req.get_post_args:从内存中读取请求数据,以table格式返回
语法:args, err = ngx.req.get_post_args(max_args?) * 从内存中读取请求体,返回table数据 * max_args表示最多读取的参数个数 环境:rewrite_by_lua*、access_by_lua*、 content_by_lua*、log_by_lua*、 header_filter_by_lua*、body_filter_by_lua*
响应头操作
ngx.header.headerName:添加、修改、删除响应头
语法:ngx.header.headerName = value、ngx.header["headerName"] = value * headerName存在,表示修改响应头 * headerName不存在,表示添加响应头 * value设置为nil,表示删除响应头 * ngx.header.test_value = value:输出时,会自动转换为test-value = value 环境:rewrite_by_lua*、access_by_lua*、 content_by_lua*、log_by_lua*、 header_filter_by_lua*、body_filter_by_lua*
ngx.resp.get_headers:获取响应头
语法:headers = ngx.resp.get_headers(max_headers?, raw?) * 读取响应头,以table类型返回 环境:rewrite_by_lua*、access_by_lua*、 content_by_lua*、log_by_lua*、balancer_by_lua*、 header_filter_by_lua*、body_filter_by_lua*
响应体操作
ngx.print、ngx.say:异步输出响应体
ngx.flush:强制刷新print、say的内容,转换为同步输出
语法格式: * ngx.print(...):异步输出,等待内容全部到达缓冲区后输出 * ngx.say(...):异步输出,等待内容全部到达缓冲区后输出 * ngx.flush():强制刷新,flush将之前的内容立刻输出
使用示例
default.conf
server { listen 80; server_name localhost; location /test { content_by_lua_block { ngx.say("hello"); ngx.sleep(3); ngx.say("gtlx"); } } location /test2 { content_by_lua_block { ngx.say("hello"); ngx.flush(); ngx.sleep(3); ngx.say("gtlx 2"); } } location /test3 { content_by_lua_block { ngx.say("hello"); ngx.flush(true); ngx.sleep(3); ngx.say("gtlx 3"); } } #error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/openresty/nginx/html; } }
创建容器
docker run -it -d -p 3000:80 \ -v /Users/huli/lua/openresty/conf7/default.conf:/etc/nginx/conf.d/default.conf \ --name open4 lihu12344/openresty
使用测试
# 全部内容等待3s后输出 huli@hudeMacBook-Pro conf7 % curl localhost:3000/test hello gtlx # hello先输出,gtlx 2等待3s后输出 huli@hudeMacBook-Pro conf7 % curl localhost:3000/test2 hello gtlx 2 # hello先输出,gtlx 2等待3s后输出 huli@hudeMacBook-Pro conf7 % curl localhost:3000/test3 hello gtlx 3
使用示例 2
default.conf
server { listen 80; server_name localhost; location /test { client_body_buffer_size 1k; client_max_body_size 1k; content_by_lua_block { ngx.req.read_body() ngx.say(ngx.req.get_body_data()) } } location /test2 { client_body_buffer_size 1k; client_max_body_size 1k; content_by_lua_block { ngx.req.read_body(); local args, err = ngx.req.get_post_args(); if args then for key,value in pairs(args) do if type(value) == "table" then ngx.say(key, " ==> ", table.concat(value, " ")) else ngx.say(key, " ==> ", value) end end else local file = ngx.req.get_body_file() if file then ngx.say("file body", file) end end } } #error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/openresty/nginx/html; } }
创建容器
docker run -it -d -p 4000:80 \ -v /Users/huli/lua/openresty/conf7/default.conf:/etc/nginx/conf.d/default.conf \ --name open5 lihu12344/openresty
***********
使用测试
# /test huli@hudeMacBook-Pro conf7 % curl -i localhost:4000/test -d "test=123&test=234&name=瓜田李下" HTTP/1.1 200 OK Server: openresty/1.21.4.1 Date: Tue, 05 Jul 2022 12:56:50 GMT Content-Type: application/octet-stream Transfer-Encoding: chunked Connection: keep-alive test=123&test=234&name=瓜田李下 # /test2 huli@hudeMacBook-Pro conf7 % curl -i localhost:4000/test2 -d "test=123&test=234&name=瓜田李下" HTTP/1.1 200 OK Server: openresty/1.21.4.1 Date: Tue, 05 Jul 2022 12:56:32 GMT Content-Type: application/octet-stream Transfer-Encoding: chunked Connection: keep-alive name ==> 瓜田李下 test ==> 123 234
get 请求
post form数据
post json数据
评论暂时关闭