# Redis2\_pass with \<host\>:\<port\> from $args fail

**URL:** <https://www.ruby-forum.com/t/redis2-pass-with-host-port-from-args-fail/218751>\
**Category:** NGINX\
**Created:** [April 10, 2012, 10:33pm UTC](https://www.ruby-forum.com/t/redis2-pass-with-host-port-from-args-fail/218751 "2012-04-10T22:33:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 10, 2012, 10:33pm UTC](https://www.ruby-forum.com/t/redis2-pass-with-host-port-from-args-fail/218751/1 "2012-04-10T22:33:27Z")

</div>

I’m trying to address several redis instances, dispatching it on a  
host:port basis (one single backend location)

here part of my nginx.conf location  
…  
location /redis\_backend {  
internal;  
set\_unescape\_uri $verb $arg\_verb;  
set\_unescape\_uri $key $arg\_key;  
set\_unescape\_uri $r\_host $arg\_host;  
set\_unescape\_uri $r\_port $arg\_port;  
redis2\_query $verb $key;  
redis2\_pass $r\_host:$r\_port;  
}  
…

I call /redis\_backend location from a lua like this:

…  
r\_instance[“redis\_host”] = ‘127.0.0.1’  
r\_instance[“redis\_port”] = ‘33001’  
…  
local res = ngx.location.capture (  
“/redis\_backend”,  
{ args =  
{ verb = r\_verb,  
key = redis\_key,  
host = r\_instance[“redis\_host”],  
port = r\_instance[“redis\_port”],  
}  
}  
)  
…

in nginx error log (i use the openresty suite ngx\_openresty/1.0.10.44),  
I  
got this error

\*2012/04/07 19:25:03 [error] 11715#0: _1 redis2: upstream  
“127.0.0.1:33001”  
not found, client: 127.0.0.1, server: …_

I’m sure redis instance is up&running. I’m able to connect to her via  
redis-cli or, simply setting redis2\_pass 127.0.0.1:33001;

ciao  
massimo

---

<div class="post-metadata">

**Author:** ![massimo\_ferrari](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9f8e36/32.png) [@massimo\_ferrari](https://www.ruby-forum.com/u/massimo_ferrari)\
**Post date:** [April 12, 2012, 5:36am UTC](https://www.ruby-forum.com/t/redis2-pass-with-host-port-from-args-fail/218751/2 "2012-04-12T05:36:00Z")

</div>

On Tue, Apr 10, 2012 at 1:32 PM, massimo ferrari  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> set\_unescape\_uri $r\_port $arg\_port;  
> redis2\_query $verb $key;  
> redis2\_pass $r\_host:$r\_port;  
> }

The redis2\_pass directive only supports values with variable  
interpolation  
to be the name of an already defined upstream. For example:

```
upstream foo1 {
    server 10.32.26.7:6379;
}

upstream foo2 {
    server 10.32.26.7:6379;
}

http {
    server {
        location /foo {
            set $n 2;
            redis_pass "foo$n";
        }
    }
}

```

Your way will not work as expected.

If really you want truly dynamic redis backends (you do not want to  
redefine them in nginx.conf or something like that), you can have a look  
at  
the lua-resty-redis library which supports exactly what you want in your  
example:

> **[GitHub - openresty/lua-resty-redis: Lua redis client driver for the ngx\_lua based on...](https://github.com/openresty/lua-resty-redis)**
>
> Lua redis client driver for the ngx\_lua based on the cosocket API

Best,  
-agentzh
