Home Use variable in lua and nginx
Reply: 1

Use variable in lua and nginx

user9099802 Published in 2018-02-12 13:38:27Z

I have a variable $aet that I initialize in lua, but I wish I could use it in nginx too.

Here is my code:

location /getIp {
    default_type 'application/json';
    rds_json          on;

    content_by_lua '
        if ngx.var.host:match("(.*).nexus$") ~= nil then
            aet = ngx.var.host:match("(.-)%.")
            $aet = aet;
    postgres_pass     database;
    postgres_query  "SELECT ip FROM establishment_view WHERE aet = $aet";
    postgres_output rds;

It does not work because in the query it does not know the variable aet :

nginx: [emerg] unknown "aet" variable

Alexander Altshuler
Alexander Altshuler Reply to 2018-02-13 10:11:04Z

RTFM https://github.com/openresty/lua-nginx-module#ngxvarvariable

Read and write Nginx variable values.

value = ngx.var.some_nginx_variable_name ngx.var.some_nginx_variable_name = value

Note that only already defined nginx variables can be written to. For example:

location /foo { set $my_var ''; # this line is required to create $my_var at config time content_by_lua_block { ngx.var.my_var = 123; ... } }

That is, nginx variables cannot be created on-the-fly.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.257793 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO