我如何在 nginx 的 lua 中使用 `encode_base64` 字符串?
How can I `encode_base64` string in lua in nginx?
我在 nginx 中使用 lua,下面是编码字符串的代码:
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)
'
启动服务器后出现以下错误:
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"
我使用此文档 https://github.com/openresty/lua-nginx-module#set_by_lua 中的语法,并且在设置变量时不使用 =
符号。我做错了什么?
同样,您犯了一些错误。 Lua 字符串连接运算符是 ..
。 Lua 不希望运算符之间有分号。你有一个奇怪的组合 lua 和 nginx 配置语法。如果您在其他地方不需要那些 $es_username
和 $es_pwd
变量,请使用
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)
';
如果您在其他地方需要这些变量,那么您的方法是
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)';
我在 nginx 中使用 lua,下面是编码字符串的代码:
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)
'
启动服务器后出现以下错误:
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"
我使用此文档 https://github.com/openresty/lua-nginx-module#set_by_lua 中的语法,并且在设置变量时不使用 =
符号。我做错了什么?
同样,您犯了一些错误。 Lua 字符串连接运算符是 ..
。 Lua 不希望运算符之间有分号。你有一个奇怪的组合 lua 和 nginx 配置语法。如果您在其他地方不需要那些 $es_username
和 $es_pwd
变量,请使用
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)
';
如果您在其他地方需要这些变量,那么您的方法是
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)';