Assign the Lua function to Nginx variable

1.4k Views Asked by At

I would like to assign the value to the nginx variable.
This is my sample code.

location / {
    set $TOKEN;
    content_by_lua_block {
        result = io.popen("echo 'https://google.com'") # or any command that will return value to result
        ngx.var.TOKEN = result:read()
    }
    proxy_pass ${TOKEN};

Do anyone have idea about it?

1

There are 1 best solutions below

0
On BEST ANSWER

Use set_by_lua_block:

location / {
    set $proxy '';              
    set_by_lua_block $proxy {
        local result = io.popen("echo 'https://www.google.com'")
        return result:read()
    }
    proxy_pass $proxy;
}