<!-- review: finished -->

<a id="external-redis2"></a>

# Redis2

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

<a id="installation-23"></a>

## Installation

To [install](https://en.angie.software//angie/docs/installation/index.md#install-packages) the module, use one of the following packages:

- Angie: `angie-module-redis2`
- Angie PRO: `angie-pro-module-redis2`

<a id="loading-the-module-23"></a>

## 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](https://en.angie.software//angie/docs/installation/external-modules/set-misc.md#external-set-misc) module:

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

<a id="configuration-example-97"></a>

## Configuration Example

```nginx
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.

<a id="request-execution-demonstration"></a>

## Request Execution Demonstration

Examples of working with the module.

```console
$ 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.

```console
$ 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.

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

Executing arbitrary Redis commands:

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

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

<a id="additional-information-24"></a>

## Additional Information

Detailed documentation and source code are available at:
[https://github.com/openresty/redis2-nginx-module](https://github.com/openresty/redis2-nginx-module).
