Redis2#

The redis2 module lets Angie use a Redis 2.x server as an upstream. Angie builds the Redis commands from the request data and returns the Redis reply to the client unparsed, in the Redis protocol format. The module implements the full unified Redis 2.0 protocol, including pipelining.

Installation#

To install the module, use one of the following packages:

  • Angie: angie-module-redis2

  • Angie PRO: angie-pro-module-redis2

Loading the Module#

To work with the module, it must be loaded in the main{} context. 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 a key value
    location /foo {
        set $value 'first';
        redis2_query set one $value;
        redis2_pass redis_upstream;
    }

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

    # Set a 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 a 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 an arbitrary command passed in a 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.

Request Execution Demonstration#

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#

Version: v0.15

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