当前位置:网站首页>openresty ngx_ Lua request response
openresty ngx_ Lua request response
2022-07-05 22:53:00 【o_ Guatian Lixia_ o】
openresty ngx_lua Request and response
Request header operation
ngx.req.set_header: add to 、 Modify request header
Grammar format :ngx.req.set_header(name, value)
* name If it doesn't exist , Express addition
* name If there is , To modify
Usage environment :set_by_lua*、rewrite_by_lua*
access_by_lua*、content_by_lua*
header_filter_by_lua*、body_filter_by_lua*
# Example
ngx.req.set_header(name, "gtlx"): Set individual values
ngx.req.set_header(test, {"1","2"}): Use an array to set multiple values
ngx.req.clear_header: Delete request header
Grammar format :ngx.req.clear_header(name)
Usage environment :set_by_lua*、rewrite_by_lua*
access_by_lua*、content_by_lua*
header_filter_by_lua*、body_filter_by_lua*
# Example
ngx.req.clear_header(name): Directly delete the request header
ngx.req.set_header(test, nil): By setting to nil, Delete request header
ngx.req.get_headers: Get request header
Grammar format :ngx.req.get_headers(max_headers?, raw?)
Usage environment :set_by_lua*、rewrite_by_lua*
access_by_lua*、content_by_lua*
header_filter_by_lua*、body_filter_by_lua*
# Example
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"]) # Get the name name Request header for
# Equate to :ngx.var.http_name
}
}
Ask for gymnastics
lua_need_request_body: Force get request body , It is not read by default
Grammar format :lua_need_request_body on|off
* off( Default ): Indicates that the request body data is not read , adopt ngx.var.request_body The data read is empty
* on: Enable reading request body data ,ngx_lua It is not recommended to read the request body in this way
matters needing attention
* If the read request body data exceeds client_body_buffer_size size ,
$request_body、ngx.var.request_body The data read is empty
* client_body_buffer_size、client_max_body_size The settings are the same , Avoid this situation
ngx.req.read_body: Open read request body
grammar :ngx.req.read_body()
Environmental Science :rewrite_by_lua*、access_by_lua*、content_by_lua*
* Read the request body synchronously , And will not block nginx The event loop
ngx.req.get_body_file: Read the request body data from the temporary file
grammar :file_name = ngx.req.get_body_file()
Environmental Science :rewrite_by_lua*、access_by_lua*、content_by_lua*
* Get the temporary file storing the requested data , And read the data , Return... As a string
* Try to control the size of the request body , Avoid using temporary files
ngx.req.get_body_data: Read the request body from memory , Return string
grammar :data = ngx.req.get_body_data()
Environmental Science :rewrite_by_lua*、access_by_lua*、content_by_lua*、log_by_lua*
* Read the request body from memory , Return string
ngx.req.get_post_args: Read request data from memory , With table Format return
grammar :args, err = ngx.req.get_post_args(max_args?)
* Read the request body from memory , return table data
* max_args Indicates the maximum number of parameters read
Environmental Science :rewrite_by_lua*、access_by_lua*、
content_by_lua*、log_by_lua*、
header_filter_by_lua*、body_filter_by_lua*
Response header operation
ngx.header.headerName: add to 、 modify 、 Delete response header
grammar :ngx.header.headerName = value、ngx.header["headerName"] = value
* headerName There is , Indicates modifying the response header
* headerName non-existent , Indicates adding a response header
* value Set to nil, Indicates deleting the response header
* ngx.header.test_value = value: When the output , Will automatically switch to test-value = value
Environmental Science :rewrite_by_lua*、access_by_lua*、
content_by_lua*、log_by_lua*、
header_filter_by_lua*、body_filter_by_lua*
ngx.resp.get_headers: Get response header
grammar :headers = ngx.resp.get_headers(max_headers?, raw?)
* Read response header , With table Type return
Environmental Science :rewrite_by_lua*、access_by_lua*、
content_by_lua*、log_by_lua*、balancer_by_lua*、
header_filter_by_lua*、body_filter_by_lua*
Response body operation
ngx.print、ngx.say: Asynchronous output response body
ngx.flush: Forced to refresh print、say The content of , Convert to synchronous output
Grammar format :
* ngx.print(...): Asynchronous output , Wait for the contents to reach the buffer and output
* ngx.say(...): Asynchronous output , Wait for the contents to reach the buffer and output
* ngx.flush(): Forced to refresh ,flush Output the previous content immediately
Examples of use
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;
}
}
Create a container
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
Use tests
# Wait for everything 3s Post output
[email protected] conf7 % curl localhost:3000/test
hello
gtlx
# hello First, the output ,gtlx 2 wait for 3s Post output
[email protected] conf7 % curl localhost:3000/test2
hello
gtlx 2
# hello First, the output ,gtlx 2 wait for 3s Post output
[email protected] conf7 % curl localhost:3000/test3
hello
gtlx 3
Examples of use 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;
}
}
Create a container
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
***********
Use tests
# /test
[email protected] conf7 % curl -i localhost:4000/test -d "test=123&test=234&name= liable to lay oneself open to suspicion "
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= liable to lay oneself open to suspicion
# /test2
[email protected] conf7 % curl -i localhost:4000/test2 -d "test=123&test=234&name= liable to lay oneself open to suspicion "
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 ==> liable to lay oneself open to suspicion
test ==> 123 234
get request


post form data


post json data


边栏推荐
- TCC of distributed solutions
- 如何快速理解复杂业务,系统思考问题?
- Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology
- 点到直线的距离直线的交点及夹角
- Distributed solution selection
- New 3D particle function in QT 6.3
- Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
- Editor extensions in unity
- Overview of Fourier analysis
- 2022软件测试工程师涨薪攻略,3年如何达到30K
猜你喜欢

Qtquick3d real time reflection

傅里叶分析概述

My experience and summary of the new Zhongtai model

Starting from 1.5, build a micro Service Framework -- log tracking traceid

opencv 判断点在多边形内外

Hcip day 12 (BGP black hole, anti ring, configuration)

2022.02.13 - SX10-30. Home raiding II

【无标题】
![[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]](/img/f4/4d09dc05f5789b980ebd23cc352f8b.jpg)
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]

航海日答题小程序之航海知识竞赛初赛
随机推荐
傅里叶分析概述
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
TOPSIS code part of good and bad solution distance method
C language - structural basis
Function default parameters, function placeholder parameters, function overloading and precautions
2022软件测试工程师涨薪攻略,3年如何达到30K
一文搞定垃圾回收器
请求二进制数据和base64格式数据的预览显示
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
利用LNMP实现wordpress站点搭建
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
[groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
Qtquick3d real time reflection
Element positioning of Web Automation
Arduino measures AC current
Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
Binary tree (III) -- heap sort optimization, top k problem
VOT Toolkit环境配置与使用