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 then
ngx.say("request error:",err)
return
end
--获取状态码
ngx.status = resp.status --获取响应信息
--响应头中的Transfer-Encoding和Connection可以忽略,因为这个数据是当前server输出的。
--获取遍历返回的头信息 for k, v in pairs(resp.headers) do
if k ~= "Transfer-Encoding" and k ~= "Connection" 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() # 后台日志
# /usr/local/openresty/nginx]# tail -f logs/debug.log
// :: [warn] #: * [lua] testhttp02.lua:: one:ufe-result: A6, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
// :: [warn] #: * [lua] testhttp02.lua:: one:Date: Fri, Aug :: GMT, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
// :: [warn] #: * [lua] testhttp02.lua:: 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"
// :: [warn] #: * [lua] testhttp02.lua:: one:Connection: keep-alive, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
// :: [warn] #: * [lua] testhttp02.lua:: one:EagleEye-TraceId: 0bfa16f315665542845385751e42fa, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
// :: [warn] #: * [lua] testhttp02.lua:: one:Strict-Transport-Security: max-age=, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
// :: [warn] #: * [lua] testhttp02.lua:: one:Content-Length: , client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
// :: [warn] #: * [lua] testhttp02.lua:: one:Timing-Allow-Origin: *, client: 192.168.10.164, server: www.server1.com, request: "GET /testhttp02 HTTP/1.1", host: "192.168.10.164"
// :: [warn] #: * [lua] testhttp02.lua:: 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 ;
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 then
ngx.say("request error:",err)
return
end
--获取状态码
ngx.status = resp.status --获取响应信息
--响应头中的Transfer-Encoding和Connection可以忽略,因为这个数据是当前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()

配置示例

# cat /usr/local/openresty/nginx/conf/nginx.conf
worker_processes ; #pid logs/nginx.pid;
events {
worker_connections ;
} http {
include mime.types;
default_type text/html; sendfile on;
keepalive_timeout ;
resolver 8.8.8.8; server {
listen ;
server_name www.server1.com;
lua_ssl_verify_depth ;
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 /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 then
ngx.say("request error:",err)
return
end
--获取状态码
ngx.status = resp.status --获取响应信息
--响应头中的Transfer-Encoding和Connection可以忽略,因为这个数据是当前server输出的。
--获取遍历返回的头信息 for k, v in pairs(resp.headers) do
if k ~= "Transfer-Encoding" and k ~= "Connection" 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()

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开发系列30--openresty中使用全局缓存

    openresty开发系列30--openresty中使用全局缓存 Nginx全局内存---本地缓存 使用过如Java的朋友可能知道如Ehcache等这种进程内本地缓存.Nginx是一个Master进 ...

  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开发系列16--lua中的控制结构if-else/repeat/for/while

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

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

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

随机推荐

  1. Andrew Ng机器学习 二: Logistic Regression

    一:逻辑回归(Logistic Regression) 背景:假设你是一所大学招生办的领导,你依据学生的成绩,给与他入学的资格.现在有这样一组以前的数据集ex2data1.txt,第一列表示第一次测验 ...

  2. [LeetCode] 0752. Open the Lock 打开转盘锁

    题目 You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', ' ...

  3. Kotlin扩展作用域分析与扩展的根本作用解析

    在上一次https://www.cnblogs.com/webor2006/p/11219358.html学习了Kotlin的扩展,继续这个话题继续拓展,首先提出这么一个问题:假如我们扩展的方法跟类中 ...

  4. linux系统编程综合练习-实现一个小型的shell程序(二)

    上节minishell当中,已经初步实现了一个简单命令的解析,这节来继续对更加复杂命令进行解析,包含:输入重定向的解析.管道行的解析.输出重定向的解析以及是否有后台作业的解析,如下: 下面对其进行实现 ...

  5. vue 博客知识点汇总

    1. vue修改url,页面不刷新 项目中经常会用到同一个页面,结构是相同的,我只是在vue-router中通过添加参数的方式来区分状态,参数可以在页面跳转时带上params,或者query,但是有一 ...

  6. 创建型模式(五) 原型模式(Prototype)

    一.动机(Motivation) 在软件系统中,经常面临着"某些结构复杂的对象"的创建工作:由于需求的变化,这些对象经常面临着剧烈的变化,但是它们却拥有比较稳定一致的接口.如何应对 ...

  7. Linux PXE 网络装机

    一.基础网络建设 Linux配置静态IP-192.168.5.1 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ONBOOT= ...

  8. Mysql 查询阻塞和事物情况

    MYSQL 服务器逻辑架构图 连接/线程处理 == > (解析器 –> 查询缓存) ===> 优化器 ===> 存储引擎 服务器级别锁MYSQL 使用的锁类型:表锁(显式:LO ...

  9. codepush安装

    https://github.com/lisong/code-push-server/blob/master/docs/README.md =====> 安装mysql下载mysql yum r ...

  10. idea-git同步服务器代码后撤销操作

    工具:IntelliJ IDEA 2019.2.1 x64 记录一次不小心同步代码后,如何撤销操作. 1.同步服务器代码,右击项目,点击Synchronize 'xxx',如下: 2.打开Versio ...