当前位置:网站首页>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


边栏推荐
- 链表之双指针(快慢指针,先后指针,首尾指针)
- MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
- Thinkphp5.1 cross domain problem solving
- APK加固技术的演变,APK加固技术和不足之处
- 分布式解决方案选型
- C language - structural basis
- Distributed solution selection
- Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
- Binary tree (III) -- heap sort optimization, top k problem
- [error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
猜你喜欢

Element positioning of Web Automation

终于搞懂什么是动态规划的

Spectrum analysis of ADC sampling sequence based on stm32

MCU case -int0 and INT1 interrupt count

2022软件测试工程师涨薪攻略,3年如何达到30K

Metaverse ape received $3.5 million in seed round financing from negentropy capital

Distance from point to line intersection and included angle of line

Ultrasonic sensor flash | LEGO eV3 Teaching

SPSS analysis of employment problems of college graduates

【无标题】
随机推荐
I closed the open source project alinesno cloud service
Go language learning tutorial (XV)
Binary tree (III) -- heap sort optimization, top k problem
Usage Summary of scriptable object in unity
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
Overview of Fourier analysis
The introduction to go language is very simple: String
Yiwen gets rid of the garbage collector
Nangou Gili hard Kai font TTF Download with installation tutorial
VOT toolkit environment configuration and use
H5c3 advanced - player
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
Vcomp110.dll download -vcomp110 What if DLL is lost
Unity Max and min constraint adjustment
fibonacci search
2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
Postman核心功能解析-参数化和测试报告