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

                 

                 

         

                 

原网站

版权声明
本文为[o_ Guatian Lixia_ o]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207052243546945.html