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. UVA816 Abbott's Revenge (三元组BFS)

    题目描述: 输入输出: 输入样例: SAMPLE 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ...

  2. LFU(最近最不常用)实现(python)

    from collections import defaultdict, OrderedDict class Node: __slots__ = 'key', 'val', 'cnt' def __i ...

  3. 重装windows10系统的教程

    1.首先从官网上下载一个win10的系统, 2.准备一个8GB的移动U盘,下载好的镜像文件烧录在这个U盘, 3.按照不同型号的机型,使用不同的按键进入BOIS模式,然后选中U盘作为启动项,读取出来这个 ...

  4. 《逆袭团队》第七次作业:团队项目设计完善&编码

    实验十一 团队作业7:团队项目设计完善&编码 内容 项目 软件工程 任课教师博客主页链接 作业链接地址 团队作业7:团队项目设计完善&编码 团队名称 逆袭团队 具体目标 (1)完善团队 ...

  5. 01.Vue前端路由学习目标

    路由的基本概念与原理 vue-router的基本使用 vue-router嵌套路由 vue-router动态路由匹配 vue-router命名路由 vue-router编程式导航 基于vue-rout ...

  6. PPT扁平化设计总结

    注:以下内容基本都来自知乎,由于已经不记得网址了,所以未能附上所有相关链接,抱歉. PPT扁平化设计原则一.亲密:意思相近的内容放在一起二.对齐:页面上的某两个元素之间总是围绕一条直线对齐三.对比:有 ...

  7. 【二分答案】Expanding Rods POJ 1905

    题目链接:http://poj.org/problem?id=1905 题目大意:原长度为L的线段因受热膨胀为一段弧,线段L.弧长L'.温度n.膨胀率c满足L' =(1+n/c)*L;求线段的中点移动 ...

  8. Tensorflow细节-P84-梯度下降与批量梯度下降

    1.批量梯度下降 批量梯度下降法是最原始的形式,它是指在每一次迭代时使用所有样本来进行梯度的更新.从数学上理解如下: 对应的目标函数(代价函数)即为: (1)对目标函数求偏导: (2)每次迭代对参数进 ...

  9. python - 使用psutils

    oshelper.py #encoding=utf-8 import psutil import datetime #查看cpu的信息 print u"CPU 个数 %s"%psu ...

  10. 通过map文件找程序崩溃的代码行

    一,配置vs 二,程序崩溃界面 // ConsoleApplication1.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include &l ...