Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在nginx Lua处理请求时会修改变量?_Nginx_Lua_Openresty - Fatal编程技术网

为什么在nginx Lua处理请求时会修改变量?

为什么在nginx Lua处理请求时会修改变量?,nginx,lua,openresty,Nginx,Lua,Openresty,我刚开始学习Lua 每次请求时,我都要检查请求参数中的name参数。然而,事实上,我们发现self.name偶尔会发生变化 比如说, request A with params: request_id = 123 & name = ABC, request B with params: request_id = 321 & name = EFG, 在日志中,我发现有请求\u id=123,但是name=EFG 为什么呢?我的课写错了吗 以下是示例代码: main.lua:

我刚开始学习Lua

每次请求时,我都要检查请求参数中的name参数。然而,事实上,我们发现self.name偶尔会发生变化

比如说,

request A with params: request_id = 123 & name = ABC, 
request B with params: request_id = 321 & name = EFG, 
在日志中,我发现有
请求\u id=123
,但是
name=EFG

为什么呢?我的课写错了吗


以下是示例代码:

main.lua:

local checker = require "checker"
local ch = checker:new()

    
if ch:check_name() then
    ngx.header["Content-Type"] = "application/json"
    ngx.status = ngx.HTTP_FORBIDDEN
    ngx.exit(ngx.HTTP_FORBIDDEN)
end
checker.lua:

local utils = require "utils"


local _M = {}


function _M:new()
    local o = {}
    setmetatable(o, self)
    self.__index = self

    self.args = utils.get_req_args() or {}

    local name = self.args["name"] or ""
    local request_id = self.args["request_id"] or ""

    self.name = name
    return o
end

function _M:check_name()
    ngx.log(ngx.ERR, "request_id: ", self.request_id, " name: ", self.name)
    
    -- do some check ...
end

乌提斯·卢阿

local json = require "cjson"

local _M = {}

function _M.new(self)
    return self
end


function _M.get_req_args()
    -- GET 
    local args = nil
    if ngx.var.request_method == "GET" then
        args = ngx.req.get_uri_args()
    -- POST 
    elseif ngx.var.request_method == "POST" then
        ngx.req.read_body()
        local data = ngx.req.get_body_data()
        args = json.decode(data)
    end
    return args
end

在checker.lua的构造函数(
\u M:new
)中,
self
是“类”对象(即
\u M
表)。您应该设置
o
的属性(
name
args
等),而不是
self
的属性
o
是新创建的实例。它似乎是由您提到的原因引起的。我已经修好了,正在观察中。非常感谢您的帮助。在checker.lua中,在构造函数(
\M:new
)中,
self
是“类”对象(即
\M
表)。您应该设置
o
的属性(
name
args
等),而不是
self
的属性
o
是新创建的实例。它似乎是由您提到的原因引起的。我已经修好了,正在观察中。非常感谢你的帮助。