openresty use http module

OpenResty default does not provide Http client, you need to use third-party plug-ins 
we can search for the appropriate client from the github, such as HTTPS: // github.com/pintsized/lua-resty-http 

Installation: Lua -resty- HTTP / lib / Resty / http.lua http_headers.lua directory and 
two files are copied to / usr / local / openresty / lualib / to the Resty directory 
(assuming openResty installation directory / usr / local / openresty) 

local RES , ERR = httpc: REQUEST_URI (URI, {   
    Method = " the POST / the GET " , --- request mode 
    Query = STR, --- GET parameter transfer mode 
    body = STR, --- POST-parameters manner 
    path = " URL "---- Path 
    headers = {--- header parameter 
        [ " the Content-the Type " ] = " file application / JSON " ,   
    }   
})   


Example: preparation of simulated query request Lynx

 - introducing http module 
local http = the require ( " resty.http " )
 - Creating http client instance 
local httpc = http: new new () 

local RESP, ERR = httpc: REQUEST_URI ( " https://list.tmall.com " , 
{ 
    Method, = " GET " , - -Request method
     --path = " /search_product.htm?q=ipone " , 
    Query = " Q = iPhone " , --- GET parameter transfer mode 
    body = " name = 'Jack' = 18 is & Age " , --- POST mode parameters passed 
    path = " /search_product.htm " , ---- path
     --- header parameter 
    headers = {[ " the User-- Agent " ] = " the Mozilla / 5.0 (the Windows NT 6.1; the WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 40.0.2214.111 Safari / 537.36 "}
})
IF Not RESP the then 
    ngx.say ( " Request error: " , ERR) 
    return 
End
 - Get Status Code 
ngx.status = resp.status

 - Get Information Response
 - Transfer- response header Encoding Connection and can be ignored, because this data is current server output.
- obtaining the header information through the returned 

for K, V in pairs (resp.headers) do 
    IF K = ~ " Transfer-Encoding " and K ~ = " Connection "  the then 
        ngx.header [K] = V 
    End 
    IF type (V ) =="table" then  
        ngx.log(ngx.WARN,"table:"..k, ": ", table.concat(v, ", "))  
    else  
        ngx.log(ngx.WARN,"one:"..k, ": ", v)  
    end  
end
ngx.say("end")
--响应体
ngx.say(resp.body)

httpc:close()


httpc:Close () 

-f logs /tail/ usr / local / openresty / Nginx] #
#
# background logdebug.log 
2019/08/23 17:58:04 [warn] 25373#0: *131 [lua] testhttp02.lua:32: one:ufe-result: A6, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
2019/08/23 17:58:04 [warn] 25373#0: *131 [lua] testhttp02.lua:32: one:Date: Fri, 23 Aug 2019 09:58:04 GMT, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
2019/08/23 17:58:04 [warn] 25373#0: *131 [lua] testhttp02.lua:32: one:Location: https://login.taobao.com/jump?target=https%3A%2F%2Flist.tmall.com%2Fsearch_product.htm%3Ftbpm%3D1%26q%3Diphone, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
2019/08/23 17:58:04 [warn] 25373#0: *131 [lua] testhttp02.lua:32: one:Connection: keep-alive, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
2019/08/23 17:58:04 [warn] 25373#0: *131 [lua] testhttp02.lua:32: one:EagleEye-TraceId: 0bfa16f315665542845385751e42fa, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
2019/08/23 17:58:04 [warn] 25373#0: *131 [lua] testhttp02.lua:32: one:Strict-Transport-Security: max-age=0, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
2019/08/23 17:58:04 [warn] 25373#0: *131 [lua] testhttp02.lua:32: one:Content-Length: 0, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
2019/08/23 17:58:04 [warn] 25373#0: *131 [lua] testhttp02.lua:32: one:Timing-Allow-Origin: *, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
2019/08/23 17:58:04 [warn] 25373#0: *131 [lua] testhttp02.lua:32: one:Server: Tengine/Aserver, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"


-------------------------------------------------- ----- 
found error 
Request error: NO defined the resolver to resolve " list.tmall.com " 


this error is because you want to configure DNS resolvers the resolver 8.8 . 8.8 , otherwise the domain name is unresolved. 
In nginx.conf profile http module add the Resolver 8.8 . 8.8 ; IP address of the DNS server provided by Google free of 
Once configured, restart nginx


 ------------------- -------------------------------------- 

access https error because we https access, you need to configure ssl certificate 
in nginx configuration file, server virtual host module disposed 

lua_ssl_verify_depth 2 ; 
lua_ssl_trusted_certificate " /etc/ssl/certs/ca-bundle.crt " ;


-------------------------------------------------- ------ 

http module application scenarios are many, here only a brief introduction about using the http module 

, there are many openresty module, can refer to HTTPS: // github.com/bungle/awesome-resty 



to suning.com example: 

http local = The require ( " resty.http " )
 - Creating http client instance 
local httpc = http: new new () 

local RESP, ERR = httpc: REQUEST_URI ( " http://issm.suning.com " , 
{ 
    Method, = " GET " , 
    path = " /productDetail_P11271.htm " , 
    headers{= [ " The User-- Agent " ] = " the Mozilla / 5.0 (the Windows NT 6.1; the WOW64) AppleWebKit / 537.36 (KHTML, like the Gecko) the Chrome / 40.0.2214.111 Safari / 537.36 " } 
}) 
IF Not RESP the then 
    ngx.say ( " Request error: " , ERR) 
    return 
End
 - Get status code 
ngx.status = resp.status

 - Get information response
 - Transfer- response header Encoding Connection and can be ignored, because the output data is the current server.
for K, V in pairs (resp.headers) do 
    IF K = ~ " Transfer-Encoding" and k ~= "Connection" then
        ngx.header[k] =v
    end
end

--响应体
ngx.say(resp.body)

httpc:close()

Guess you like

Origin www.cnblogs.com/reblue520/p/11402286.html