lua使用resty.http做nginx反向代理(https请求,docker容器化部署集群),一个域名多项目转发

news/2024/6/18 21:43:50 标签: lua, 开发语言

下载使用
链接:https://pan.baidu.com/s/1uQ7yCzQsPWsF6xavFTpbZg
提取码:htay
–来自百度网盘超级会员V5的分享

在根目录下执行:
# 从 github 上下载文件
git clone https://github.com/ledgetech/lua-resty-http.git
# 将 lua-resty-http/lib/ 下的 resty 文件夹上传至服务器一下目录下(lua-5.4.2安装目录下)
/lua-5.4.2/
# 此模块主要用来发送 http 请求
# 也可选用 socket.http

在这里插入图片描述
ad_load.lua文件

ngx.header.content_type="application/json;charset=utf8"
local cjson = require("cjson")
local https = require 'resty.http'
local json = cjson:new()
--服务器一,域名拼接request_uri获得请求的路由和路由上的参数
-- local server_one_url = 'http://127.0.0.1:9059/index01';
-- local server_one_url = 'http://127.0.0.1:9059'..ngx.var.request_uri;
local server_one_url = ngx.var.url_ip..':'..ngx.var.url_port..ngx.var.request_uri;
local server_one_code = 'ONE_CODE';
-- ngx.say(server_one_url)
-- ngx.exit(ngx.OK)

--http get 请求方法
local function http_get(url)
    local http = https:new()
    local res, err = http:request_uri(url, {
        method = 'GET',
        headers = {
            ['Content-Type'] = 'application/json';
        }
    })
    if res.status == 200 then
        ngx.say(res.body)
    else
        -- ngx.say(res.status)
        ngx.say('服务器错误!')
    end
end

-- http post 请求方法
local function http_post(url, data)
    local http = https:new()
    local res, err = http:request_uri(url, {
        method = 'POST',
        body = data,
        headers = {
            ['Content-Type'] = 'application/json';
        }
    })
    if res.status == 200 then
        ngx.say(res.body)
    else
        ngx.say('服务器错误!')
    end
end

-- ngx.say(server_one_url)
-- ngx.say(ngx.var.request_method)
-- ngx.exit(ngx.OK)
if ngx.var.request_method == "GET" then
    --解析参数并做转发
    http_get(server_one_url)
elseif ngx.var.request_method == "POST" then
    --获取请求参数
    ngx.req.read_body()
    local data = ngx.req.get_body_data()
    --解析参数并做转发
    http_post(server_one_url, data)
end







    location / {
      if ($server_port = 9058 ) {
        # 定义使用内网ip,定义请求端口
        set $url_ip 'http://127.0.0.1';
        set $url_port '9059';
        content_by_lua_file /lua-5.4.2/ad_load.lua;
      }
    }

在这里插入图片描述


http://www.niftyadmin.cn/n/5331897.html

相关文章

面经-redis

什么是Redis Redis(Remote Dictionary Server)键只能为字符串,值:字符串、列表、集合、散列表、有序集合。Redis 用来做分布式锁。支持事务 、持久化、LUA脚本、LRU驱动事件、多种集群方案。 Redis为什么这么快 完全基于内存,数据结构简单…

Unreal Engine(UE5)中构建离线地图服务

1. 首先需要用到3个软件,Unreal Engine,gis office 和 bigemap离线服务器 Unreal Engine下载地址:点击前往下载页面 Gis office下载地址:点击前往下载页面 Bigemap离线服务器 下载地址: 点击前往下载页面 Unreal Engine用于数字孪生项目开发&#x…

el-dialog嵌套使用,只显示遮罩层的问题

直接上解决方法 <!-- 错误写法 --><el-dialog><el-dialog></el-dialog></el-dialog><!-- 正确写法 --><el-dialog></el-dialog><el-dialog></el-dialog>我是不建议嵌套使用的&#xff0c;平级也能调用&#xff0c…

TypeError the JSON object must be str, bytes or bytearray, not ‘list‘

在使用python的jason库时&#xff0c;偶然碰到以下问题 TypeError: the JSON object must be str, bytes or bytearray, not ‘list’ 通过如下代码可复现问题 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> import json >>> ra json.loads(a) Trac…

ArcGIS Pro 如何新建布局

你是否已经习惯了在ArcGIS中数据视图和布局视图之间来回切换&#xff0c;到了ArcGIS Pro中却找不到二者之间切换的按钮&#xff0c;即使新建布局后却发现地图怎么却是一片空白。 这一切的一切都是因为ArcGIS Pro的功能框架完全不同&#xff0c;这里为大家介绍一下在ArcGIS Pro…

取消lodash.throttle中的默认执行完最后一次函数

我的场景: 我有一个列表,我考虑用户连续点击删除的情况&#xff0c;如果用户连续点击&#xff0c;可能会导致数据库中的数据被删除了&#xff0c;但是我还需要刷新数据列表才能反应到页面上&#xff0c;可是这时候用户又点击了同一条数据的删除按钮多次&#xff0c;导致发起了…

开发安全之:Cookie Security: Cookie not Sent Over SSL

Overview 程序创建了 Cookie&#xff0c;但未将 <code>Secure</code> 标记设置为 <code>true</code>。 Details 现今的 Web 浏览器支持每个 cookie 的 Secure 标记。如果设置了该标记&#xff0c;那么浏览器只会通过 HTTPS 发送 cookie。通过未加密…

ABAP IDOC 2 XML

有个需求&#xff0c;外围系统希望我们给到一个IDOC 记录的样例&#xff0c;但是我们we02中并无法看到 就找了一个demo去直接展示IDOC内容 *&---------------------------------------------------------------------* *& Report Z_IDOC_TO_XML *&------------…