3

I am using redis2-nginx-module to serve html content stored as a value in redis. Following is the nginx config code to get value for a key from redis.

  redis2_query get $fullkey;                                     
  redis2_pass     localhost:6379;                                
  #default_type text/html;

When the url is hit the following unwanted response is rendered along with the value for that key.

$14

How to remove this unwanted output? Also if key passed as an argument doesn't exist in the redis, how to check this condition and display some default page?

Ajeet Khan
  • 8,582
  • 8
  • 42
  • 65

1 Answers1

0

(Here's a similar question on ServerFault)

There's no way with just redis2 module, as it always return a raw Redis response.

If you only need GET and SET commands you may try with HttpRedisModule (redis_pass). If you need something fancier, like hashes, you should probably try filtering the raw response from Redis with Lua, e.g. something along the lines of

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))
';

(Sorry, the code's untested, don't have an OpenResty instance at hand.)

Community
  • 1
  • 1
drdaeman
  • 11,159
  • 7
  • 59
  • 104
  • Thanx for the answer. I couldn't find any installation guide for lua-resty-redis. Can you suggest me some blogpost or installation instruction? – Ajeet Khan May 13 '16 at 16:36
  • Sorry, don't have any. When I had experimented with nginx+Lua I've basically built nginx with `--add-module=path/to/lua-module` among the other options, in a same manner [official docs suggest](https://github.com/openresty/lua-nginx-module#installation). – drdaeman May 13 '16 at 16:40
  • Can you help out in this - http://stackoverflow.com/questions/37480357/404-page-not-found-when-a-url-is-hit-but-properly-served-when-opened-from-the-li – Ajeet Khan May 29 '16 at 05:32