I am using lua inside nginx, below is the code to encode a string:
set_by_lua $base64_credential '
set $es_username os.getenv("ES_USERNAME");
set $es_pwd os.getenv("ES_PWD");
return ngx.encode_base64(ngx.var.es_username ":" ngx.var.es_pwd)
'
after launching the server I got below error:
2021/11/18 01:58:01 [error] 7#7: *151 failed to load inlined Lua code: set_by_lua:2: '=' expected near '$', client: 10.0.6.61, server: localhost, request: "GET /health HTTP/1.1", host: "10.0.1.246:8080"
I use the syntax from this doc https://github.com/openresty/lua-nginx-module#set_by_lua and it doesn't use =
sign when set a variable. What did I do wrong?
CodePudding user response:
Again, you made a couple of errors. Lua operator for string concatenation is ..
. Lua doesn't expect semicolons between the operators. You have a weird mix of lua and nginx configuration syntax. If you don't need those $es_username
and $es_pwd
variables elsewhere, use
set_by_lua $base64_credential '
local es_username = os.getenv("ES_USERNAME")
local es_pwd = os.getenv("ES_PWD")
return ngx.encode_base64(es_username .. ":" .. es_pwd)
';
If you need those variables elsewhere, then your way is
set_by_lua $es_username 'return os.getenv("ES_USERNAME")';
set_by_lua $es_pwd 'return os.getenv("ES_PWD")';
set_by_lua $base64_credential 'return ngx.encode_base64(ngx.var.es_username .. ":" .. ngx.var.es_pwd)';