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 ...
随机推荐
- day16-Servlet05
Servlet05 14.HttpServletRequest HttpServletRequest对象代表客户端的请求 当 客户端/浏览器 通过HTTP协议访问服务器时,HTTP请求头中的所有信息都 ...
- 基于python的数学建模---多模糊评价
权重 ak的确定--频数统计法 选取正整数p的方法 画箱形图 取1/4与3/4的距离(IQR) ceil()取整 代码: import numpy as np def frequency(mat ...
- sublime text设置build system automatic提示no build system
解决办法: 将: "selector": "source.asm" 改为: "selector": ["source.asm&qu ...
- 关于linux更改root用户下面的鼠标样式
前言 这几天一直研究关于 lightmd 显示管理器(也就是刚进入系统的用户登录界面)的主题配置问题,我发现鼠标样式和登录个人用户的鼠标样式不一样(之前我也发现了,懒得捣鼓)今天抽出时间研究了一下,这 ...
- python-函数的参数与返回值
Python函数 4.1.函数初识 在编写程序的过程中,有某一功能代码块出现多次,但是为了提高编写的效率以及代码的重用,所以把具有独立功能的代码块组织为一个小模块,这就是函数 就是一系列Python语 ...
- Linux系统下安装tomcat步骤
安装参考教程:https://www.cnblogs.com/li150dan/p/12535067.html 说明:jdk自动安装后路径是/usr/lib/jvm 在"vim /etc/p ...
- js中数组追加和删除
1.push 往后追加 let arr=[1,2,3,4];arr.push(5);console.log(arr);//得到[1,2,3,4,5] 2.unshift方法 往前追加 let arr ...
- 3.8:使用R语言实现Apriori算法示例
〇.目标 1.使用R语言实现Apriori算法完成关联规则挖掘:2.利用超市购物篮Groceries数据进行关联规则分析. 一.利用arules包加载Groceries数据集 二.探索和准备数据 三. ...
- JavaEE Day13 Tomcat和Servlet
之前是web基础,现在是web核心 今日内容: web相关概念的回顾 开源的web服务器软件:Tomcat Servlet:整个web技术的核心[Servlet入门] 一.web相关概念的回顾 1.软 ...
- live-player live-pusher惨案
昨天遇到的问题,旧项目: 一个页面同时使用live-player和live-pusher时候遇到的问题,live-pusher正常,live-player无效,没有任何报错 打log 所有livepl ...