我在 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 中的語法,并且=在設定變數時不使用符號。我做錯了什么?
uj5u.com熱心網友回復:
同樣,你犯了幾個錯誤。字串連接的 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)';
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/360689.html
