local s = ngx.socket.tcp()
local host = "142.162.233.150"
local port = 80
local ok, err = s:connect(host,port)
if not ok then
ngx.say("failed to connect: " .. err)
return
end
local data = ""
.. "Host: domain.cn\r\n"
.. "Connection: close\r\n"
.. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"
.. "User-Agent: Nginx-lua\r\n"
.. "Accept-Language: zh-CN,zh;q=0.8,en;q=0.6\r\n\r\n"
local bytes, err = s:send(data)
ngx.say(bytes)
if not bytes then
ngx.say("failed to send query")
return
end
local read_headers = s:receiveuntil("\r\n\r\n")
local headers, err = read_headers()
if not read_headers then
ngx.say("failed to read response headers:", err)
return
end
ngx.say(headers)
local body,err = s:receive("*a")
if not body then
ngx.say("failed to read response body", err)
return
end
ngx.say(body)
local ok, err = s:setkeepalive(60000,8)
if not ok then
ngx.say("failed to put the connection into pool "
.. "with pool capacity 8 "
.. "and maximal idle time 60 sec"
)
return
end
[root@bogon ~]# time curl 10.0.10.17/time
206
HTTP/1.1 200 OK
Server: ngx_openresty
Date: Tue, 02 Feb 2016 02:21:16 GMT
Content-Type: application/octet-stream
Content-Length: 14
Connection: close
1454379676.240
failed to put the connection into pool with pool capacity 8 and maximal idle time 60 sec
real 0m0.025s
user 0m0.006s
sys 0m0.002s
[root@bogon ~]# time curl 10.0.10.17/time
211
HTTP/1.1 200 OK
Server: ngx_openresty
Date: Tue, 02 Feb 2016 02:12:22 GMT
Content-Type: application/octet-stream
Content-Length: 14
Connection: keep-alive
1454379142.900
real 1m0.025s
user 0m0.005s
sys 0m0.006s