nginx通过lua-nginx-module自定义转发请求

nginx通过lua-nginx-module自定义转发请求。
!!!注意事项:
1.当请求body大于 client_body_buffer_size 默认值8k或16k时,请求报文将会被nginx缓存到硬盘,此时 ngx.req.get_body_data() 无法获取到body正文。请修改nginx client_body_buffer_size 128k,或者更大。典型案例:如果是转发multipart/form-data类型,不去修改大小,就会转发失败。
2.Web 应用服务器-OpenResty

zdy_proxy.lua

local cookie = require "resty.cookie"
local http = require "resty.http"
local upload = require "resty.upload"
local json = require "cjson"
local resp = require "ngx.resp"
local redis = require "resty.redis"

-- redis
local function redis_func(key, value, expires_in)
    local rds = redis:new()
    if not rds then
        error("rds is nil")
    end
    rds:set_timeout(1000) -- 设置超时时间为 1000 毫秒

    local ok, err = rds:connect("127.0.0.1", 6379) -- 连接 Redis 服务器
    if not ok then
        rds:close() -- 连接失败需要及时关闭释放连接
        error("failed to connect : " .. err)
    end

    local res, err = rds:auth("123456") -- 身份认证
    if not res then
        error("failed to authenticate: " .. err)
    end

    ngx.log(ngx.DEBUG, "SET " .. key .. " " .. value .. " EX " .. expires_in)
    ok, err = rds:set(key, value, "EX", expires_in) -- 向 Redis 写入数据
    if not ok then
        error("failed to set dog: " .. err)
    end
    ngx.log(ngx.DEBUG, "set result: ", ok)

    rds:close() -- 关闭连接
    return ok
end

-- 输入响应内容,ngx.exit退出
local function response_func(resp_status, resp_headers, resp_body)
    ngx.status = resp_status
    --ngx.header = resp_headers -- !!!这种方式无法设置响应头,暂时未找到bug原因
    for v, m in pairs(resp_headers
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值