Nginx-redis 模块返回字符串长度以及来自 Redis 的值

标签 nginx redis openresty

我正在使用 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 .

如果您只需要 GETSET 命令,您可以尝试使用 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/

相关文章:

c - 将数据从 C 传递到 R

javascript - 带有 sinon 返回函数的 stub 函数?

nginx - 为什么我不能在 init_by_lua 阶段使用 ngx.socket.tcp() ?

nginx - 为什么当我尝试挂载卷时 Docker 无法打开 nginx.conf?

express - 将本地 nginx 服务器部署到公共(public) ubuntu 16.04

linux - 无法在 fedora22 上启动 nginx 服务器

Nginx:具有粘性 session 的最少连接

django - 如何将 Django-Timeline 连接到 Redis 后端?

ssl - 使用 openresty 实现 ssl 时 AWS EC2 实例出现 sni-support-required-for-valid-ssl 错误

docker - 如何为 ssl_certificate 动态设置域