<!-- review: finished -->

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

# Keyval

The module allows the use of variables with values from "key-value" pairs,
which are stored in shared memory or in a Redis store.

<a id="installation-15"></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-keyval`
- Angie PRO: `angie-pro-module-keyval`

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

## Loading the Module

To work with the module, it must be loaded in the `main{}` context:

```nginx
load_module modules/ngx_http_keyval_module.so;
```

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

## Configuration Example

```nginx
keyval_zone zone=one:32k;
keyval $arg_key $value zone=one;

server {
    listen 80;
    server_name localhost;

    location /get {
        return 200 "key '$arg_key' has value = '$value'\\n";
    }

    location /set {
        set $value $arg_value;
        return 200 "'$arg_key' key added with '$arg_value' value\\n";
    }
}
```

Adding and modifying entries in the shared memory zone 'one' is done by
assigning a value to the `$value` variable. The key value
is stored in the `$arg_key` variable. In this configuration, this is done via the
`set` directive:

```nginx
set $value $arg_value;
```

<a id="demonstration-1"></a>

## Demonstration

Let's define some values using requests:

```console
$ curl "localhost/set/?key=one&value=TextForKeyOne"

  'one' key added with 'TextForKeyOne' value
```

```console
$ curl "localhost/set/?key=two&value=TextForKeyTwo"

  'two' key added with 'TextForKeyTwo' value
```

Let's check:

```console
$ curl "localhost/get/?key=one"

  key 'one' has value = 'TextForKeyOne'
```

```console
$ curl "localhost/get/?key=two"

  key 'two' has value = 'TextForKeyTwo'
```

<a id="using-redis"></a>

## Using Redis

Let's modify the configuration to store "key-value" pairs in a Redis store:

```nginx
keyval_zone_redis zone=oneredis;
keyval $arg_key $value zone=oneredis;

server {
    listen 80;
    server_name localhost;

    location /get {
        return 200 "key '$arg_key' has value = '$value'\\n";
    }

    location /set {
        set $value $arg_value;
        return 200 "'$arg_key' key added with '$arg_value' value\\n";
    }
}
```

Let's add a "key-value" pair to the Redis store via a request:

```console
$ curl "localhost/set/?key=one&value=TextForKeyOne"

  'one' key added with 'TextForKeyOne' value
```

The same can be done using Redis itself:

```console
$ redis-cli

  127.0.0.1:6379> set oneredis:two 'text for key two'

  OK

  127.0.0.1:6379>
```

Let's check:

```console
$ redis-cli --scan

  "oneredis:one"
  "oneredis:two"
```

```console
$ curl "localhost/get/?key=one"

  key 'one' has value = 'TextForKeyOne'
```

```console
$ curl "localhost/get/?key=two"

  key 'two' has value = 'text for key two'
```

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

## Additional Information

A complete description of the directives and the source code is available at:
[https://github.com/kjdev/nginx-keyval](https://github.com/kjdev/nginx-keyval).
