【问题标题】:fetch parameters passed with url into nginx.conf by lua in Openresty在 Openresty 中通过 lua 获取通过 url 传递的参数到 nginx.conf
【发布时间】:2013-11-26 00:20:23
【问题描述】:

我有一个 url '/gifts/' 下面是管理逻辑的 nginx.conf 文件的代码。

位置/礼物{ default_type 文本/html; 设置 $target ''; content_by_lua ' 本地 redis = 需要“resty.redis”; 本地红色 = redis:new() 红色:set_timeout(1000) -- 1 秒 本地正常,错误 = 红色:连接(“127.0.0.1”,6379) 如果不行的话 ngx.log(ngx.ERR, err, "Redis 连接失败") 返回 ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE) 结尾 本地 ok1, err = red:set("Animal", "DOG") 如果不行的话 ngx.say("redis设置缓存失败", err) 结尾 本地资源, err = red:get("Animal") 如果不是 res 那么 ngx.say("失败", err) 结尾 ngx.say("动物", res) '; }

/gifts/ 对我来说很好用 但是我有一个要求,例如我想将参数提取到此登录中,例如 /gifts?key=name&value=Prashant 我想获取键和值的值。

【问题讨论】:

    标签: nginx lua


    【解决方案1】:

    我使用 req.get_uri_args() 来获取url中传递的所有参数。

    本地参数 = ngx.req.get_uri_args()

    【讨论】:

    • 它对我不起作用......它给出了 500 个内部服务器错误。你能详细分享一下你是怎么做到的吗?
    • 请告诉我您在哪个上下文中使用它。?您只能将它与 set_by_lua、rewrite_by_lua、access_by_lua、content_by_lua、header_filter_by_lua、body_filter_by_lua、log_by_lua 一起使用
    • 我改用ngx.var.uri() 解决了这个问题。还是谢谢你:)
    【解决方案2】:

    看看ngx.req.get_uri_args(),这将返回一个包含所有当前请求 URL 查询参数的 Lua 表。

    例子:

     location = /test {
        content_by_lua '
            local args = ngx.req.get_uri_args()
            for key, val in pairs(args) do
                if type(val) == "table" then
                    ngx.say(key, ": ", table.concat(val, ", "))
                else
                    ngx.say(key, ": ", val)
                end
            end
        ';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多