如何在 openresty 中使用 lua 代码在运行时获取匹配的 location_name

在 rewrite_by_lua 阶段,我想要在运行时获取匹配的 location 名称。

比如: nignx 服务器配置:

server {
    listen 80;
    server_name 127.0.0.1;
    location /test {
        return 200 "test";
    }
    location /test/api {
        return 200 "test";
    }
    location ~ /id/\d* {
        return 200 "api";
    }
}

如果我访问 http://127.0.0.1/test/api/abc,我想要得到 nginx 的 location 名称:/test/api

如果我访问 http://127.0.0.1/test/ddd,我想要得到 nginx 的 location 名称:/test

如果我访问 http://127.0.0.1/id/123,我想要得到 nginx 的 location 名称:/id/\d*

openresty 版本:1.19.3.1

你能帮我吗?

点赞