如何解决Lua - 连接变量对转义特殊字符 - Openresty Nginx TLDR完整说明
我不使用 Lua,但需要将它与链接中提供的 Openresty (nginx) 一起使用。
Openresty 有一个 lua 模块 我设法安装并正确运行了 Openresty nginx 版本,网站正常运行。
此 answer 显示了如何将标题连接成字符串 $request_headers
:
set_by_lua $request_headers '
local h = ngx.req.get_headers()
local request_headers_all = ""
for k,v in pairs(h) do
request_headers_all = request_headers_all .. "["..k..": "..v..\n"]"
end
return request_headers_all
';
我在上面的 lua 函数中将格式从 ""..k..": "..v..";"
更改为 "["..k..": "..v.."]"
。
日志格式:
log_format log_realip 'Host: "$host",Via : "$http_via",RemoteAddr: "$remote_addr",XForwardedFor: "$h
ttp_x_forwarded_for",'RealIPRemoteAddr: "$realip_remote_addr" - $status - "$request_uri" - **"[HEADERS]" - $request_headers';**
Host: "192.168.1.4",Via : "-",//trimmed just to show the [HEADERS]
....
"[HEADERS]" - [sec-ch-ua: \x22Chromium\x22;v=\x2288\x22,\x22Google Chrome\x22;v=\x228
8\x22,\x22;Not A Brand\x22;v=\x2299\x22][sec-ch-ua-mobile: ?0][cookie: __utmz=abcdef; frontend=abcdef; adminhtml=abcdef
08; TestName=Some Value][upgrade-insecure-requests: 1][accept-language: en-US,en;q=0.9][user-agent: Mozilla/5.0
(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/88.0.4324.104 Safari/537.36][accept
-encoding: gzip,deflate,br][accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/we
bp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9][sec-fetch-dest: document][host: 192.168.1.4][se
c-fetch-user: ?1][connection: keep-alive][sec-fetch-mode: navigate][cache-control: max-age=0][sec-fetch-site: n
one
当将 log_format
与 $request_headers
字符串一起使用时,我将所有标题放在一行中,但我正在尝试创建一个 newline \n
以将字符串分解为线。上面的示例是我添加 \n
但似乎没有将中断输出到日志文件的地方。
我知道 request_headers_all ..
连接字符串,但是 key k
和 value v
在这里发生了什么:""..k..": "..v..\n""
?
"..variablename.."
在做什么,这是在 Lua 字符串中总是使用变量的方式吗?
如何在该字符串中创建换行符?或者 nginx(openresty) 有没有可能不输出换行符?
解决方法
request_headers_all = request_headers_all .. "["..k..": "..v..\n"]"
包含语法错误。将 "["..k..": "..v..\n"]"
替换为 "["..k..": "..v.."]\n"
换行符需要在引号内,因为它是字符串的一部分,在括号后添加新行可能是有意义的。
“..variablename..”是做什么的,这就是变量总是这样 在 Lua 字符串中使用?
在变量上使用连接运算符连接一个字符串值,连接它的字符串表示如果它是一个数字或调用 __concat
或如果这两个都不为真则引发错误。
阅读https://www.lua.org/manual/5.4/manual.html#3.4.6
,您将 \n
添加到错误的位置,您可以更改为
request_headers_all = request_headers_all .. "["..k..": "..v.."]\n"
用于换行日志。
在lua中,..
是一个concat操作符,用来连接字符串,例如:
print("hello" .. "world")
得到结果helloworld
。
您的代码 \n"]"
有语法错误,因为 \n
不在字符串中。
lua 字符串不能直接使用变量,通常,lua 使用 string.format
表示复杂的字符串。例如:
local test = "hello"
string.format("%s world",test) -- hello world
您可以使用 string.format 来连接字符串。
此外,您可以使用 table.concat
来连接字符串。
例如:
local test = {}
table.insert(test,"hello")
table.insert(test,"world")
local concated_string = table.concat(test,' ')
print(concated_string) -- hello world
,
这里的主要问题/问题是包装器软件如何处理换行符/转义符,它是否期待 "\n"
?还是期待"\r\n"
?
最终,新行在被解释和打印之前实际上并不存在,并且您正在创建一个从 Lua 引擎返回到包装器的大量字符串,因此如何解释新行取决于包装器软件。
编辑:我错过了使用其他解析函数已经回答了这个问题。
另外文档状态使用原解析函数转义需要双重转义。
Here,\\\\d+ is stripped down to \\d+ by the Nginx config file parser and this is further stripped down to \d+ by the Lua language parser before running.
以上答案给了我一些指导,但建议的格式仍然不起作用。在玩弄 string.format("%s %s\n",k,v)
、string.format("%s %s\\n",v)
之后,我仍然遇到 unfinished string
错误或没有换行符输出。 (试图在第二个示例中转义字符串)。
根据给出的答案,我假设答案给出了正确的 lua 信息,因此决定很可能 lua + openresty
做一些不同的事情。
我将尝试更新标题以反映更具体的要求
TLDR
-
Openresty + lua
带有特殊字符的字符串操作可能无法按预期工作,即使使用string.format()
- 从返回字符串的
set_by_lua
更改为set_by_lua_block
以允许更好的string.format()
或字符串连接。 - 使用您的自定义/现有
log_format
更新 nginx 配置并添加开关escape=none
。
完整说明
调查提供的链接答案 set_by_lua
函数文档:
注意 在 v0.9.17 版本之后不鼓励使用此指令。改用 set_by_lua_block 指令。
因此来自链接中的原始 set_by_lua
:
set_by_lua $request_headers '
return 'stringvalue'
';
我改为set_by_lua_block
函数:
该指令将 Lua 源代码直接内联在一对花括号 ({}) 中,而不是在 Nginx 字符串文字中(需要特殊字符转义)
set_by_lua_block $request_headers{
local h = ngx.req.get_headers()
local request_headers_all = ""
for k,v in pairs(h) do
local rowtext = ""
rowtext = string.format("[%s %s]\n",v)
request_headers_all = request_headers_all .. rowtext
end
return request_headers_all
}
重要的部分是这个 _block {}
函数正确地转义了特殊字符。
之后我在日志文件中收到输出为:x0A
(换行符文字)。
最后一步是使用自定义 nginx.conf
更新 log_format
文件并添加 escape=none
:
log_format log_realip escape=none "$nginx_variables"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。