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模块相关推荐

  1. openresty开发系列36--openresty执行流程之6日志模块处理阶段

    openresty开发系列36--openresty执行流程之6日志模块处理阶段 一)header_filter_by_lua 语法:header_filter_by_lua <lua-scri ...

  2. openresty开发系列16--lua中的控制结构if-else/repeat/for/while

    openresty开发系列16--lua中的控制结构if-else/repeat/for/while一)条件 - 控制结构 if-else if-else 是我们熟知的一种控制结构.Lua 跟其他语言 ...

  3. openresty开发系列29--openresty中发起http请求

    openresty开发系列29--openresty中发起http请求 有些场景是需要nginx在进行请求转发 用户浏览器请求url访问到nginx服务器,但此请求业务需要再次请求其他业务: 如用户请 ...

  4. openresty开发系列28--openresty中操作mysql

    openresty开发系列28--openresty中操作mysql Mysql客户端    应用中最常使用的就是数据库了,尤其mysql数据库,那openresty lua如何操作mysql呢?   ...

  5. openresty开发系列27--openresty中封装redis操作

    openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...

  6. openresty开发系列26--openresty中使用redis模块

    openresty开发系列26--openresty中使用redis模块 在一些高并发的场景中,我们常常会用到缓存技术,现在我们常用的分布式缓存redis是最知名的, 操作redis,我们需要引入re ...

  7. openresty开发系列25--openresty中使用json模块

    openresty开发系列25--openresty中使用json模块 web开发过程中,经常用的数据结构为json,openresty中封装了json模块,我们看如何使用 一)如何引入cjson模块 ...

  8. openresty开发系列24--openresty中lua的引入及使用

    openresty开发系列24--openresty中lua的引入及使用 openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua   ---> ...

  9. openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息

    openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...

  10. openresty开发系列38--通过Lua+Redis 实现动态封禁IP

    openresty开发系列38--通过Lua+Redis 实现动态封禁IP 一)需求背景为了封禁某些爬虫或者恶意用户对服务器的请求,我们需要建立一个动态的 IP 黑名单.对于黑名单之内的 IP ,拒绝 ...

最新文章

  1. React项目实践系列一
  2. 中原标准时间对时_GPS对时系统(时间同步系统)应用电子政务
  3. c语言float二进制输出代码_C语言学习笔记——学前知识概述
  4. 服务器执行sh文件或目录,将window的shell脚本通过ftp传输到Linux服务器后, shell脚本中执行时提示“没有那个文件或目录”的解决办法...
  5. 闭包 python_Python闭包思想与用法浅析
  6. 构建工具Bazel入门
  7. 画出多项式的硬件编码_硬件工程师职业规划
  8. List of Algorithms
  9. 12款响应式 Lightbox(灯箱)效果插件
  10. 用python画一个机器猫歌词_用Python语言模型和LSTM做一个Drake饶舌歌词生成器
  11. 计算机神经网络sci,神经网络方向sci期刊
  12. 5047. 多边形三角剖分的最低得分
  13. 快速入手Web幻灯片制作
  14. Ubuntu及window的配置 java变量和快捷键
  15. 苹果计算机手机号隐藏功能,苹果手机的四大隐藏功能,很多人都不知道,一起来一探究竟...
  16. Microsoft Edge浏览器文件保存位置记录
  17. Failed to start component异常的解决方案
  18. 老牌系统:雨一直下win7主题
  19. 记面陌陌科技计算机视觉算法工程师被刷经历(附面试过程中被问倒的一些算法题分析)
  20. Java泛型入参的三种通配符使用

热门文章

  1. c语言的简单题目,C语言的一些简单题目,没有答案,哪位大神帮忙做一下!!!...
  2. Java版SMS4加密算法
  3. 数据结构之希尔排序图文详解及代码(C++实现)
  4. 无线轮播android,Android无限轮播Banner的实现
  5. springboot 2.3_Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
  6. 病案编码员需要计算机的什么知识,前辈分享:优秀编码员必须经历的成长过程,你到哪一级了?...
  7. php 文字图片怎么保存为图片,php技术实现加载字体并保存成图片
  8. 回顾 | Kubernetes SIG-Cloud-Provider-Alibaba 首次网研会(含 PPT 下载)
  9. 五分钟学会使用 go modules(含在家办公使用技巧)
  10. 冒泡排序html代码,冒泡排序.html