【问题标题】:How can I `encode_base64` string in lua in nginx?如何在 nginx 的 lua 中“encode_base64”字符串?
【发布时间】:2021-12-28 23:29:51
【问题描述】:

我在 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 中的语法,并且在设置变量时不使用 = 符号。我做错了什么?

【问题讨论】:

    标签: nginx lua


    【解决方案1】:

    同样,您犯了几个错误。用于字符串连接的 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)';
    

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 1970-01-01
      • 2014-08-06
      • 2016-03-02
      • 2018-06-28
      • 1970-01-01
      • 2017-07-27
      • 2010-10-24
      • 2011-07-12
      相关资源
      最近更新 更多