Lua-转义特殊字符的变量(对)的串联-Openresty Nginx [英] Lua - Concatenation of variables (pairs) escaping special characters - Openresty Nginx

查看:99
本文介绍了Lua-转义特殊字符的变量(对)的串联-Openresty Nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不使用Lua,但需要将其与链接中提供的Openresty(nginx)一起使用.

I don't use Lua, but need to use it with Openresty (nginx) as provided in the link.

Openresty有一个lua模块 我设法正确安装并运行了Openresty nginx版本,该网站正常工作.

答案显示如何将标头连接到字符串 $ request_headers :

This answer shows how to concatenate headers into a string $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
';

我将格式从"",".. k ..":".. v ..",""更改为"" [[..].k..":上面lua函数中的..v.."]".

I changed the format from ""..k..": "..v..";" to "["..k..": "..v.."]" in the lua function above.

日志格式:

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 字符串一起使用时,我在一行中获得所有标头 ,但是我试图创建一个换行符\ n 可以将字符串分成几行.上面的示例是我添加 \ n 的地方,但似乎没有输出中断到日志文件.

When using log_format with $request_headers string I get all the headers in one line, but I am trying to create a newline \n to break the string into lines. The example above is where I added \n but doesn't seem to output break to the log file.

我了解 request_headers_all .. 将字符串连接起来,但是使用 key k value v 的情况在这里是什么:""..k ..":".. v .. \ n""?

I understand the request_headers_all .. concatenates the string, but what is happening here with the key k and value v : ""..k..": "..v..\n""?

".. variablename .." 在做什么,这是在Lua字符串中始终使用变量的方式吗?

What is the "..variablename.." doing, is this how variables are always used inside Lua strings?

如何在该字符串中创建换行符?还是nginx(openresty)不输出换行符?

How would I be able to create a line break in that string? Or is it possible that nginx(openresty) doesn't output the newline?

推荐答案

以上答案为我提供了一些指导,但建议的格式仍然无法使用.在玩弄 string.format("%s %s\n", k, v), string.format("%s %s\\n", k,v)我仍然出现未完成的字符串错误或没有换行输出.(试图在第二个示例中转义字符串).

The above answers gave me some guidance, but the formats suggested still didn't work. After playing around with string.format("%s %s\n", k, v), string.format("%s %s\\n", k, v) I still got unfinished string errors or no newline output. (Tried to escape string in second example).

根据给出的答案,我认为答案给出了正确的lua信息,因此决定 lua + openresty 可能会做一些不同的事情.

Based on the answers given I assumed the answers gave correct lua information, so decided most likely lua + openresty does something different.

我将尝试更新标题以反映更具体的要求

    即使使用 string.format(),使用特殊字符的
  1. Openresty + lua 特殊字符字符串操作也可能无法正常工作
  2. 从将字符串返回的 set_by_lua 更改为 set_by_lua_block 可以使 string.format()或字符串串联更好的
  3. 使用自定义/现有的 log_format 更新nginx配置,并添加开关 escape = none .
  1. Openresty + lua string manipulation with special characters might not work as expected, even when using string.format()
  2. Change from set_by_lua which returns a string to set_by_lua_block which allows string.format() or string concatenation better.
  3. Update nginx configuration with your custom/existing log_format and add the switch escape=none.

完整说明

调查提供的链接答案 set_by_lua 功能文档:

注意v0.9.17版本以后,不建议使用此伪指令.请改用set_by_lua_block指令.

NOTE Use of this directive is discouraged following the v0.9.17 release. Use the set_by_lua_block directive instead.

因此来自链接的原始 set_by_lua :

set_by_lua $request_headers '
  return 'stringvalue'
';

我更改为 set_by_lua_block 函数:

此伪指令直接在一对花括号({})中内联Lua源代码,而不是在Nginx字符串文字中(这需要特殊的转义字符)进行内联

this directive inlines the Lua source directly inside a pair of curly braces ({}) instead of in an Nginx string literal (which requires special character escaping)

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", k, v)
    request_headers_all = request_headers_all .. rowtext

  end
  return request_headers_all
}

重要的是,此 _block {} 函数可以正确转义特殊字符.

The important part is that this _block {} function escapes the special characters correctly.

此后,我在日志文件中收到的输出为: x0A (换行符).

After that I received output in the log files as : x0A (newline character literal).

最后一步是使用自定义的 log_format 更新 nginx.conf 文件,并添加 escape = none :

The final step then is to update the nginx.conf file with the custom log_format and add escape=none:

log_format log_realip escape=none "$nginx_variables"

这篇关于Lua-转义特殊字符的变量(对)的串联-Openresty Nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆