首页 > 系统相关 >Nginx12 openresty使用lua-resty-http模块

Nginx12 openresty使用lua-resty-http模块

时间:2022-11-30 10:44:31浏览次数:65  
标签:resty http 192.168 lua local id

1 简介

  在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

  分别看实际访问的哪个服务

标签:resty,http,192.168,lua,local,id
From: https://www.cnblogs.com/jthr/p/16937704.html

相关文章

  • java Http请求工具类
    importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importlombok.extern.slf4j.Slf4j;importjavax.net.ssl.*;importjava.io.*;importj......
  • pip Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not
      #解决方案 把…\anaconda3\Library\bin加入到系统环境变量即可。总是提示SSL有问题,然而只是SSL就在bin里边,所以没有生效。主要参考:https://github.com/conda/co......
  • 一个有趣的nginx HTTP 400响应问题分析
    背景之前在一次不规范HTTP请求引发的nginx响应400问题分析与解决中写过客户端query参数未urlencode导致的400问题,当时的结论是:对于query参数带空格的请求,由于其不符合HT......
  • C# luainterface luanet踩坑记录
      //C#调用dll传递c回调函数模板格式//LuaInterface.LuaDLL;using LuaInterface;publicpartialclassxxx{//......//C#默认情况下委托都是......
  • 使用 Hypercorn HTTP/2 ASGI 部署 FastAPI
    AnotherASGIwebserverthatsupportsHTTP/2andHTTP/3specifications我已经介绍了很多关于FastAPI的教程,其中服务器部署了Uvicorn,一个快速的ASGIWeb......
  • Lua学习笔记
    1.注释--单行注释 多行注释--[[--]] 2.变量命名最好不要使用下划线加大写字母作为标示符,因为lua内部的保留字也是这样命名的。Lua不允许使用特殊字符如@,$,和%来定......
  • 【最详细易懂】C++和Lua交互总结
    一、Lua与C++的交互机制——Lua堆栈Lua和C++的交互机制的基础在于Lua提供了一个虚拟栈,C++和Lua之间的所有类型的数据交换都通过这个栈完成。无论何时C++想从Lua中调用一个......
  • 使用socket实现http服务端
    #encoding=utf-8importreimportsocket#接收消息的方法defrecv_msg(tcp_socket,recv_data):requests=recv_data.splitlines()print(requests)......
  • .NET6之MiniAPI(二十二):HttpClient
    说明:本篇不是说明HttpClient怎么使用,而以分享在asp.netcoreminiapi框架下,HttpClient的引入和使用方式。我们在业务开发中,免不了调用三方的服务,这时就会用到Htt......
  • .NET6之MiniAPI(二十二):HttpClient
    说明:本篇不是说明HttpClient怎么使用,而以分享在asp.netcoreminiapi框架下,HttpClient的引入和使用方式。我们在业务开发中,免不了调用三方的服务,这时就会用到Htt......