Redis2#

The redis2 module provides the ability to interact with a Redis server 2.x. It implements the full unified Redis 2.0 protocol, including support for Redis pipelining.

Loading the Module#

To work with the module, it must be loaded in the context of main{}. The example below also uses directives from the set-misc module:

load_module modules/ndk_http_module.so;
load_module modules/ngx_http_set_misc_module.so;
load_module modules/ngx_http_redis2_module.so;

Configuration Example#

upstream redis_upstream {
    server 127.0.0.1:6379;
}

server {
    listen       80;
    server_name  localhost;

    # Set key value
    location /foo {
        set $value 'first';
        redis2_query set one $value;
        redis2_pass redis_upstream;
    }

    # Get value by key
    location /bar {
        redis2_query get one;
        redis2_pass redis_upstream;
    }

    # Set key value from query parameters
    location /set {
        set_unescape_uri $key $arg_key;
        set_unescape_uri $val $arg_val;
        redis2_query set $key $val;
        redis2_pass 127.0.0.1:6379;
    }

    # Get value by key from query parameters
    location /get {
        set_unescape_uri $key $arg_key;
        redis2_query get $key;
        redis2_pass 127.0.0.1:6379;
    }

    # Execute multiple pipelined commands
    location /pipeline {
        set $value 'first';
        redis2_query set one $value;
        redis2_query get one;
        redis2_query set one 'first first';
        redis2_query get one;
        redis2_pass 127.0.0.1:6379;
    }

    # Execute arbitrary command passed in query parameter
    location /cmd {
        set_unescape_uri $cmd $arg_command;
        redis2_raw_query "$cmd\r\n";
        redis2_pass 127.0.0.1:6379;
    }
}

Warning

Important! Unlike the proxy_pass directive in Angie, using variables in the parameter of the redis2_pass directive is not allowed.

Demonstrating Request Execution#

Examples of working with the module.

$ curl localhost/foo
+OK

$ curl localhost/bar
$5
first

Here $5 is the length of the value (5 bytes), and first is the value itself.

$ curl 'localhost/set/?key=two&val=second%20value'
+OK

$ curl 'localhost/get/?key=two'
$12
second value

$ curl 'localhost/get/?key=three'
$-1

The value $-1 indicates that the key three does not exist.

$ curl localhost/pipeline
+OK
$5
first
+OK
$11
first first

Executing arbitrary Redis commands:

$ curl 'localhost/cmd/?command=set%20three%20"third%20value"'
+OK

$ curl 'localhost/cmd/?command=get%20three'
$11
third value

Additional Information#

Detailed documentation and source code are available at: openresty/redis2-nginx-module.