Navid_pdp11
Navid_pdp11

Reputation: 4012

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

I want to create a Location in my Nginx with such route /resource/{{state}} though the {{state}} is a place holder for a variable that must pass to my Lua script and according to this variable I want to process some resources.

I cannot find any documentation or guideline for creating such a route in Nginx and passing path parameters to Lua. Are path parameters available in nginx and if the answer is yes how can I access them in mylua code?

Upvotes: 1

Views: 1025

Answers (1)

Dmitry Meyer
Dmitry Meyer

Reputation: 1535

Use the regex location syntax with the ngx.var.VARIABLE API:

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

Note: nginx uses the PCRE2 library for regex support. Check the documentation for the syntax.

Upvotes: 2

Related Questions