我正在使用 redis2-nginx-module提供作为值存储在 redis
中的 html 内容。以下是 nginx
配置代码,用于从 redis 获取键的值。
redis2_query get $fullkey;
redis2_pass localhost:6379;
#default_type text/html;
当点击 url 时,将呈现以下不需要的响应以及该键的值。
$14
如何删除不需要的输出?此外,如果作为参数传递的键在 Redis 中不存在,如何检查此条件并显示一些默认页面?
最佳答案
(这里是 a similar question on ServerFault )
没有办法只有 redis2 模块,因为它 always return a raw Redis response .
如果您只需要 GET
和 SET
命令,您可以尝试使用 HttpRedisModule (redis_pass
)。如果你需要更高级的东西,比如哈希,你应该试试 filtering the raw response from Redis with Lua ,例如类似的东西
content_by_lua '
local res = ngx.location.capture("/redis",
{ args = { key = ngx.var.fullkey } }
)
local body = res.body
local s, e = string.find(body, "\r\n", 1, true)
ngx.print(string.sub(body, e + 1))
';
(抱歉,代码未经测试,手头没有 OpenResty 实例。)
关于Nginx-redis 模块返回字符串长度以及来自 Redis 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37209690/