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 ...
随机推荐
- networkQuality
基本使用 networkQuality 是一个命令行工具,需要使用「终端」App(或者你首选的其他终端模拟器)运行.方法是: 首先,点按「程序坞」(Dock)中的「启动台」(LaunchPad)图标, ...
- AI音乐创作,让每一个人都成为音乐家
从录音带.MP3到专业的耳机.音箱,随着音乐消费方式的不断升级,音乐创作的专业"门槛"也在AI技术的加持下逐渐大众化,创作者的创新设计.创作频率也在持续增强,能降低创作门槛且智能化 ...
- 微信小程序的学习(二)
一.数据绑定 1.数据绑定的基本原则 在 data 中定义数据 在 wxml 中使用数据 2.如何在 data 里面定义数据? 在页面对应的 .js 文件中,把数据定义到 data 对象中即可: 3. ...
- 更换K8S证书可用期
帮助文档:https://zealous-cricket-cfa.notion.site/kubeadm-k8s-24611be9607c4b3193012de58860535e 解决: 1.安装GO ...
- oracle问题记录
ORA-01034: ORACLE not available [oracle@ilanni ~]$ sqlplus /nolog SQL*Plus: Release 10.2.0.1.0 – Pro ...
- 基于python的数学建模---scipy库
instance1: 求解下列线性规划问题 s.t. 代码: from scipy import optimizeimport numpy as npc = np.array([2,3,-5])A ...
- springboot接收前端传参的几种方式
1.通过HttpServletRequest接收,常用于获取请求头参数以及Cookie,适用于GET 和 POST请求方式,以下两种方式: @GetMapping("/demo1" ...
- VRRP原理和实战
一.VRRP基本概述 ·VRRP能够在不改变组网的情况中,将多台路由器虚拟成一个虚拟路由器,通过配置虚拟路由器的IP地址为默认网关,实现网关的备份. ·协议版本:VRRPv2(常用)和VRRPv3 · ...
- 分布式日志:Exceptionless的安装与部署
安装步骤 首先exceptionless依赖elasticsearch,而elasticsearch需要java环境,所以先安装jdk jdk下载地址:https://www.oracle.com/t ...
- 粘包、struct模块、进程并行与并发
目录 粘包现象 struct模块 粘包代码实战 udp协议(了解) 并发编程理论 多道技术 进程理论 进程并行与并发 进程的三状态 粘包现象 1.服务端连续执行三次recv 2.客户端连续执行三次se ...