nginx(openresty) lua 解决对接其他平台,响应文件中地址跨域问题

location 添加配置

复制代码
# location  添加的配置
# 作用:清空body体中的内,使得在lua处理响应体是,重新计算返回大小【如果不置空,它会保留原始响应体大小,导致处理数据的时候出现截断的问题】
header_filter_by_lua 'ngx.header.content_length = nil';
# 配置指定使用的lua脚本
body_filter_by_lua_file conf/lua_script/response.lua;

lua脚本文件

复制代码
#response.lua;脚本内容
-- 获取当前响应数据
local chunk, eof = ngx.arg[1], ngx.arg[2]

-- 定义全局变量,收集全部响应
if ngx.ctx.buffered == nil then
    ngx.ctx.buffered = {}
end

-- 如果非最后一次响应,将当前响应赋值
if chunk ~= "" and not ngx.is_subrequest then
    table.insert(ngx.ctx.buffered, chunk)

    -- 将当前响应赋值为空,以修改后的内容作为最终响应
    ngx.arg[1] = nil
end

-- 如果为最后一次响应,对所有响应数据进行处理
if eof then
    -- 获取所有响应数据
    local whole = table.concat(ngx.ctx.buffered)

    ngx.ctx.buffered = nil

    if not whole then
        ngx.log(ngx.NOTICE, "Response-Response Body Is Null: ", whole)
        return
    end

    -- 进行你所需要进行的处理
    ngx.log(ngx.NOTICE, "Response-Digest Business Param String: ", whole)

    --原始地址
    local str ="1.8.2.3:8084"
    --新地址
    local str1 = "10.14.65.129:9901"
    -- string.gsub(whole,str,str1)
    -- 重新赋值响应数据,以修改后的内容作为最终响应
    ngx.arg[1] = string.gsub(whole,str,str1)
end

场景:

在原有的平台,外挂三方的链接,三方只对服务ip开通访问权限,所有客户端访问都失败,现在采用所有访问三方的地址,都改为服务端的地址,通过服务端地址代理解决这个问题。也就是将1.8.2.3:8084替换为服务器ip10.14.65.129,然后通过nginx或者openresty代理区解决

相关推荐
小白冲鸭35 分钟前
苍穹外卖-前端环境搭建-nginx双击后网页打不开
运维·前端·nginx
oMcLin2 小时前
如何在 Debian 11 上配置并优化 Nginx 与 Lua 脚本,提升高并发网站的动态请求处理能力?
nginx·debian·lua
笑笑�3 小时前
OpenResty + nginx_upstream_check_module 构建主动健康检查模块
运维·nginx·负载均衡·openresty
源代码•宸3 小时前
goframe框架签到系统项目开发(补签逻辑实现、编写Lua脚本实现断签提醒功能、简历示例)
数据库·后端·中间件·go·lua·跨域·refreshtoken
每天回答3个问题4 小时前
Lua 函数教程
开发语言·ue5·ue4·lua
工具罗某人19 小时前
docker快速部署minio
java·nginx·docker
oMcLin21 小时前
如何在Ubuntu 24.04上通过配置Nginx与Keepalived实现高可用负载均衡集群
nginx·ubuntu·负载均衡
小Ti客栈1 天前
前后台Nginx部署同域名路径区分配置
java·前端·nginx
星空露珠1 天前
时间罗盘小界面模组
开发语言·数据结构·算法·游戏·lua
fredricen1 天前
Openwrt21.02应用日志全局开关控制
lua·openwrt·luci