> For the complete documentation index, see [llms.txt](https://docshome.gitbook.io/nginx-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docshome.gitbook.io/nginx-docs/he-xin-gong-neng/http/ngx_http_keyval_module.md).

# ngx\_http\_keyval\_module

* [指令](/nginx-docs/he-xin-gong-neng/http/ngx_http_keyval_module.md#directives)
  * [keyval](/nginx-docs/he-xin-gong-neng/http/ngx_http_keyval_module.md#keyval)
  * [keyval\_zone](/nginx-docs/he-xin-gong-neng/http/ngx_http_keyval_module.md#keyval_zone)

`ngx_http_keyval_module` 模块（1.13.3）创建的带值变量从 [API](https://github.com/DocsHome/nginx-docs/tree/f6135c42a499e9fab0adb433738fcf8cd4041627/模块参考/http/ngx_http_api_module.md#http_keyvals_) 管理的键值对中获取。

> 该模块可作为我们[商业订阅](http://nginx.com/products/?_ga=2.259594698.1917722686.1520954456-1859001452.1520648382)的一部分。

## 示例配置 <a href="#example_configuration" id="example_configuration"></a>

```
http {

    keyval_zone zone=one:32k state=one.keyval;
    keyval $arg_text $text zone=one;
    ...
    server {
        ...
        location / {
            return 200 $text;
        }

        location /api {
            api write=on;
        }
    }
}
```

## 指令 <a href="#directives" id="directives"></a>

### keyval

|       - | 说明                                    |
| ------: | ------------------------------------- |
|  **语法** | **keyval** `key $variable zone=name`; |
|  **默认** | ——                                    |
| **上下文** | http                                  |

创建一个新的变量 `$variable`，该变量的值从键值数据库中通过 `key` 查找。字符串匹配忽略大小写。数据库存储在 `zone` 参数指定的共享内存区域中。

### keyval\_zone

|       - | 说明                                              |
| ------: | ----------------------------------------------- |
|  **语法** | **keyval\_zone** `zone=name:size [state=file]`; |
|  **默认** | ——                                              |
| **上下文** | http                                            |

设置保存键值数据库的共享内存区域的名称（`name`）和大小（`size`）。键值对由 [API](https://github.com/DocsHome/nginx-docs/tree/f6135c42a499e9fab0adb433738fcf8cd4041627/模块参考/http/ngx_http_api_module.md#http_keyvals_) 管理。

可选的 `state` 参数指定一个文件，该文件将键值数据库的当前状态保持为 JSON 格式，并使其在 nginx 重启时保持不变。

## 原文档

<http://nginx.org/en/docs/http/ngx_http_keyval_module.html>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docshome.gitbook.io/nginx-docs/he-xin-gong-neng/http/ngx_http_keyval_module.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
