Nginx12 openresty使用lua-resty-http模块
1 简介
https://github.com/ledgetech/lua-resty-http
在lua中操作http请求有两种方式
第一种方式:使用通过ngx.location.capture 去方式实现
第二种方式:lua-resty-http,是用于访问外部 Http 资源,外部 web 服务,RESTFul 等的轻量级 http 库。因为openresty默认没有引入lua-resty-http,所以需要自行下载。
2 下载安装
2.1 下载解压
https://github.com/ledgetech/lua-resty-http
2.2 上传
将解压后的下面三个文件上传到openresty/lualib/resty目录下
3 http使用示例
3.1 修改nginx配置文件
在http下加一行配置
resolver 8.8.8.8;
在server加一个location配置
location /http {
default_type text/html;
content_by_lua_file lua/lua-resty-http.lua;
}
3.2 添加文件lua-resty-http.lua
内容
local http = require("resty.http") local httpc = http.new() local resp, err = httpc:request_uri("http://www.sogou.com", {
method = "GET",
path = "/web?query=resty.http",
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 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()
3.3 重启后访问
4 lua-resty-http实现一致性hash负载均衡简要示例
现在有两个服务可以访问,分别为192.168.28.111,192.168.28.112
4.1 修改ngxin配置文件
在server下加一个location
4.2 添加文件lua-resty-http-hash-lb.lua
内容
local http = require("resty.http")
local httpc = http.new()
-- 服务ip
local hosts = {"192.168.28.111","192.168.28.112"}
-- 获取请求参数id
local item_id= ngx.var.id
-- 获取id的hash值
local id_hash = ngx.crc32_long(item_id)
-- 取余
local index = (id_hash % 2) +1
-- 发起请求,根据余数确定向哪个服务发起请求
local resp, err = httpc:request_uri("http://"..hosts[index], {
method = "GET",
path = "/sogou?query=resty.http",
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.say(resp.body) httpc:close()
4.3 重启后访问
http://192.168.28.110:8099/hashlb?id=1
http://192.168.28.110:8099/hashlb?id=2
http://192.168.28.110:8099/hashlb?id=3
http://192.168.28.110:8099/hashlb?id=4
http://192.168.28.110:8099/hashlb?id=5
分别看实际访问的哪个服务
Nginx12 openresty使用lua-resty-http模块的更多相关文章
- OpenResty(nginx+lua) 入门
OpenResty 官网:http://openresty.org/ OpenResty 是一个nginx和它的各种三方模块的一个打包而成的软件平台.最重要的一点是它将lua/luajit打包了进来, ...
- LUA+resty 搭建验证码服务器
使用Lua和OpenResty搭建验证码服务器 雨客 2016-04-08 16:38:11 浏览2525 评论0 云数据库Redis版 摘要: Lua下有个Lua-GD图形库,通过简单的Lua语句就 ...
- (转)OpenResty(nginx+lua) 开发入门
原文:https://blog.csdn.net/enweitech/article/details/78519398 OpenResty 官网:http://openresty.org/ Open ...
- CentOS安装OpenResty(Nginx+Lua)开发环境
一.简介 OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高 ...
- openresty开发系列21--lua的模块
openresty开发系列21--lua的模块 从lua5.1开始,Lua 加入了标准的模块管理机制,Lua 的模块是由变量.函数等已知元素组成的 table, 因此创建一个模块很简单,就是创建一个 ...
- 给lnmp一键包中的nginx安装openresty的lua扩展
lnmp一键包(https://lnmp.org)本人在使用之后发现确实好用,能帮助我们快速搭建起lnmp.lamp和lnmpa的web生产环境,因此推荐大家可以多试试.但有的朋友可能需要使用open ...
- 【原创】大叔问题定位分享(36)openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil
openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil This function returns nil if the request ...
- 高并发 Nginx+Lua OpenResty系列(3)——模块指令
Nginx Lua 模块指令 Nginx共11个处理阶段,而相应的处理阶段是可以做插入式处理,即可插拔式架构:另外指令可以在http.server.server if.location.locatio ...
- lua resty template && openresty 使用
1. 安装 luarocks install lua-resty-template 2. 使用 配置模板页面位置 有多种方式: a. 直接使用root 目录 代码如下: ...
- Openresty(Lua+Nginx)实践
简介: OpenResty(也称为 ngx_openresty)是一个全功能的 Web 应用服务器.它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项. OpenRest ...
随机推荐
- golang 简书
https://www.jianshu.com/p/548adff0d10d Go 入门指南 https://github.com/wuxiaoxiaoshen/go-example-for-live ...
- 2022csp普及组真题:解密(decode)
2022csp普及组真题:解密(decode) 题目 [题目描述] 给定一个正整数 k,有 k 次询问,每次给定三个正整数 ni , ei , di,求两个正整数 pi , qi, 使 ni = pi ...
- DP?
杨斌涵//aad69d38 分治优化DP 分治优化1D/1D dp 对于一类 \[f(x) = \min_{k = y}^{x - 1} w(l, r) \] 即所有 \(w(l,r)\) 事先已知, ...
- 【Devexpress】pivotGridControl设置不显示展开折叠按钮
只需要设置.效果看图二
- oracle 中模糊查询对like的代替insrt()函数 可以做到效率节约一倍以上
昨天在处理一个字符拆分的功能时,用用到了insrt()函数,偶然发现其实特可以代替模糊查询的like,经多次测试可节约效率一倍以上. 代码如下: select distinct(a.deptname) ...
- Spring Boot实现任意位置的properties及yml文件内容配置与获取
〇.参考资料 1.Spring Boot 中文乱码问题解决方案汇总 https://blog.51cto.com/u_15236724/5372824 2.spring boot读取自定义配置prop ...
- 【SQL进阶】【分步写、联合各自排序、TIMESTAMPDIFF时间比较】Day04:多表查询
〇.内容 时间比较2-2 联合结果各自排序 查询列和GROUP BY 一.嵌套子查询 1.月均完成试卷数不小于3的用户爱作答的类别 自己的答案[错误]: SELECT tag, COUNT(A.sta ...
- 网络编程 - OSI七层协议详解
目录 网络编程基础 软件开发架构 网络编程简介 OSI七层协议简介 OSI协议之物理连接层 OSI协议之数据链路层 网络相关专业名词 OSI之网络层 OSI协议之传输层 传输层之TCP协议/UDP协议 ...
- Python3.7.3环境搭建
Python3.7.3安装(Win10) 到2019年初,Python3已经更新到了Python3.7.3,Python有两个大版本Python2和Python3,Python3是现在和未来的主流. ...
- [python] 基于matplotlib实现圆环图的绘制
圆环图本质上是一个中间切出一块区域的饼状图.可以使用python和matplotlib库来实现.本文主要介绍基于matplotlib实现圆环图.本文所有代码见:Python-Study-Notes # ...