如何通过 nginx 将 Path 参数传递给 lua 代码?

How can I pass Path parameters to lua code by nginx?

我想在我的 Nginx 中使用这样的路径创建一个位置 /resource/{{state}} 尽管 {{state}} 是一个变量的占位符必须传递给我的 Lua 脚本,根据这个变量我想处理一些资源。

我找不到任何关于在 Nginx 中创建这样的路由并将路径参数传递给 Lua 的文档或指南。 nginx 中是否提供路径参数?如果是,我如何在 mylua 代码中访问它们?

使用正则表达式 location syntax with the ngx.var.VARIABLE API:

location ~ ^/resource/(?<state>[^/]+)/?$ {
    content_by_lua_block {
        ngx.say(ngx.var.state)
    }
}

注意:nginx 使用 PCRE2 library for regex support. Check the documentation 语法。