openresty开发系列30--openresty中使用http模块
生活随笔
收集整理的這篇文章主要介紹了
openresty开发系列30--openresty中使用http模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
OpenResty默認沒有提供Http客戶端,需要使用第三方提供的插件
我們可以從github上搜索相應的客戶端,比如https://github.com/pintsized/lua-resty-http
安裝方法:將 lua-resty-http/lib/resty/ 目錄下的 http.lua 和 http_headers.lua
兩個文件拷貝到 /usr/local/openresty/lualib/resty 目錄下即可
(假設 OpenResty 安裝目錄為 /usr/local/openresty)local res, err = httpc:request_uri(uri, { method = "POST/GET", ---請求方式query = str, ---get方式傳參數body = str, ---post方式傳參數path = "url" ----路徑headers = { ---header參數["Content-Type"] = "application/json", }
}) 示例:編寫模擬請求天貓的查詢--引入http模塊
local http = require("resty.http")
--創建http客戶端實例
local httpc = http:new()local resp,err = httpc:request_uri("https://list.tmall.com",
{method = "GET", ---請求方式--path="/search_product.htm?q=ipone",query="q=iphone", ---get方式傳參數body="name='jack'&age=18", ---post方式傳參數path="/search_product.htm", ----路徑---header參數headers = {["User-Agent"]="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"}
})
if not resp thenngx.say("request error:",err)return
end
--獲取狀態碼
ngx.status = resp.status--獲取響應信息
--響應頭中的Transfer-Encoding和Connection可以忽略,因為這個數據是當前server輸出的。
--獲取遍歷返回的頭信息for k, v in pairs(resp.headers) doif k ~= "Transfer-Encoding" and k ~= "Connection" thenngx.header[k] =vendif 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()# 后臺日志
# /usr/local/openresty/nginx]# tail -f logs/debug.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" -------------------------------------------------------
發現報錯
request error :no resolver defined to resolve "list.tmall.com"此錯誤是因為要配置DNS解析器resolver 8.8.8.8,否則域名是無法解析的。
在nginx.conf配置文件中 http模塊加上resolver 8.8.8.8; Google提供的免費DNS服務器的IP地址
配置好后,重啟nginx---------------------------------------------------------訪問https錯誤,因為我們訪問的https,需要配置ssl證書
在nginx配置文件中,server虛擬主機模塊設置lua_ssl_verify_depth 2;
lua_ssl_trusted_certificate "/etc/ssl/certs/ca-bundle.crt";--------------------------------------------------------http模塊應用場景很多,這里只簡單介紹了一下http模塊的使用還有很多openresty模塊,可以參考 https://github.com/bungle/awesome-resty
以suning.com為例:local http = require("resty.http")
--創建http客戶端實例
local httpc = http:new()local resp,err = httpc:request_uri("http://issm.suning.com",
{method = "GET",path="/productDetail_P11271.htm",headers = {["User-Agent"]="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"}
})
if not resp thenngx.say("request error:",err)return
end
--獲取狀態碼
ngx.status = resp.status--獲取響應信息
--響應頭中的Transfer-Encoding和Connection可以忽略,因為這個數據是當前server輸出的。
for k,v in pairs(resp.headers) doif k ~= "Transfer-Encoding" and k ~= "Connection" thenngx.header[k] =vend
end--響應體
ngx.say(resp.body)httpc:close()
?
配置示例
# cat /usr/local/openresty/nginx/conf/nginx.conf worker_processes 4;#pid logs/nginx.pid; events {worker_connections 10240; }http {include mime.types;default_type text/html;sendfile on;keepalive_timeout 65;resolver 8.8.8.8;server {listen 80;server_name www.server1.com;lua_ssl_verify_depth 2;lua_ssl_trusted_certificate "/etc/ssl/certs/ca-bundle.crt";location /tmall {access_by_lua_file /usr/local/lua/tmall.lua;}location / {root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}lua腳本
# cat /usr/local/lua/tmall.lua --引入http模塊 local http = require("resty.http") --創建http客戶端實例 local httpc = http:new()local resp,err = httpc:request_uri("https://list.tmall.com", {method = "GET", ---請求方式--path="/search_product.htm?q=ipone",query="q=iphone", ---get方式傳參數body="name='jack'&age=18", ---post方式傳參數path="/search_product.htm", ----路徑---header參數headers = {["User-Agent"]="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"} }) if not resp thenngx.say("request error:",err)return end --獲取狀態碼 ngx.status = resp.status--獲取響應信息 --響應頭中的Transfer-Encoding和Connection可以忽略,因為這個數據是當前server輸出的。 --獲取遍歷返回的頭信息for k, v in pairs(resp.headers) doif k ~= "Transfer-Encoding" and k ~= "Connection" thenngx.header[k] =vendif 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()?
轉載于:https://www.cnblogs.com/reblue520/p/11402286.html
總結
以上是生活随笔為你收集整理的openresty开发系列30--openresty中使用http模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: openresty开发系列29--ope
- 下一篇: 子网划分与子网聚合