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 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模块的更多相关文章
- openresty开发系列36--openresty执行流程之6日志模块处理阶段
openresty开发系列36--openresty执行流程之6日志模块处理阶段 一)header_filter_by_lua 语法:header_filter_by_lua <lua-scri ...
- openresty开发系列30--openresty中使用全局缓存
openresty开发系列30--openresty中使用全局缓存 Nginx全局内存---本地缓存 使用过如Java的朋友可能知道如Ehcache等这种进程内本地缓存.Nginx是一个Master进 ...
- openresty开发系列29--openresty中发起http请求
openresty开发系列29--openresty中发起http请求 有些场景是需要nginx在进行请求转发 用户浏览器请求url访问到nginx服务器,但此请求业务需要再次请求其他业务:如用户请求 ...
- openresty开发系列28--openresty中操作mysql
openresty开发系列28--openresty中操作mysql Mysql客户端 应用中最常使用的就是数据库了,尤其mysql数据库,那openresty lua如何操作mysql呢? ...
- openresty开发系列27--openresty中封装redis操作
openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...
- openresty开发系列26--openresty中使用redis模块
openresty开发系列26--openresty中使用redis模块 在一些高并发的场景中,我们常常会用到缓存技术,现在我们常用的分布式缓存redis是最知名的, 操作redis,我们需要引入re ...
- openresty开发系列25--openresty中使用json模块
openresty开发系列25--openresty中使用json模块 web开发过程中,经常用的数据结构为json,openresty中封装了json模块,我们看如何使用 一)如何引入cjson模块 ...
- openresty开发系列24--openresty中lua的引入及使用
openresty开发系列24--openresty中lua的引入及使用 openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua ---> ...
- openresty开发系列16--lua中的控制结构if-else/repeat/for/while
openresty开发系列16--lua中的控制结构if-else/repeat/for/while 一)条件 - 控制结构 if-else if-else 是我们熟知的一种控制结构.Lua 跟其他语 ...
- openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息
openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...
随机推荐
- koa2安装
安装 1. npm install koa-generator -g 2. Koa2 test-koa2 3. npm install & npm run dev 看package.json里 ...
- 使用BERT模型生成句子序列向量
之前我写过一篇文章,利用bert来生成token级向量(对于中文语料来说就是字级别向量),参考我的文章:<使用BERT模型生成token级向量>.但是这样做有一个致命的缺点就是字符序列长度 ...
- 04 Spring的依赖注入
依赖注入:Dependency Injection.它是 spring 框架核心 ioc 的具体实现. 我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖 ...
- Oracle 序号函数
Oracle提供的序号函数:以emp表为例:1: rownum 最简单的序号 但是在order by之前就确定值.select rownum,t.* from emp t order by ename ...
- Effective C++读书笔记(转)
第一部分 让自己习惯C++ 条款01:视C++为一个语言联邦 一.要点 ■ c++高效编程守则视状况而变化,取决于你使用c++的哪一部分. 二.扩展 将c++视为一个由相关语言组成的联邦而非单一语言会 ...
- vue自定义元素拖动
岗位序列拖动交换岗位 <span draggable="true" @dragstart="onDragstart($event,index,index2)&quo ...
- Eclipse的tab键为4个空格完整方法 附:阿里代码开发规范书
开发规范书:http://pan.baidu.com/s/1dESdyox 1.点击 window->preference-,依次选择 General->Editors->Text ...
- Tensorflow 细节P-40
1.绝大部分时候都会忽略graph的使用,如下代码所示,学会怎样tf.get_default_graph()是重要的,此外注意变量定义时的初始化必须加 initializer 2.此外,要知道 wri ...
- IntelliJ IDEA 2017 MySQL5 绿色版 Spring 4 Mybatis 3 配置步骤详解(二)
前言 继续上一篇安装教程 首先是MySQL绿色版安装之后其他组件安装,如果篇幅较长会分为多篇深入讲解,随笔属于学习笔记诸多错误还望指出 共同学习. MySQL 5.7 绿色版 我本地安装的是 ...
- 44个Java性能优化
44个Java性能优化 首先,代码优化的目标是: 减小代码的体积 提高代码运行效率 代码优化细节 1 .尽量指定类.方法的final修饰符 带有final修饰符的类是不可派生的.在Java核心AP ...