# ngx\_http\_session\_log\_module

* [示例配置](/nginx-docs/he-xin-gong-neng/http/ngx_http_session_log_module.md#example_configuration)
* [指令](/nginx-docs/he-xin-gong-neng/http/ngx_http_session_log_module.md#directives)
  * [session\_log](/nginx-docs/he-xin-gong-neng/http/ngx_http_session_log_module.md#session_log)
  * [session\_log\_format](/nginx-docs/he-xin-gong-neng/http/ngx_http_session_log_module.md#session_log_format)
  * [session\_log\_zone](/nginx-docs/he-xin-gong-neng/http/ngx_http_session_log_module.md#session_log_zone)
* [内嵌变量](/nginx-docs/he-xin-gong-neng/http/ngx_http_session_log_module.md#embedded_variables)

`ngx_http_session_log_module` 模块启用会话日志（多个 HTTP 请求的聚合），而不是单个 HTTP 请求。

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

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

以下配置根据请求客户端的地址和 **User-Agent** 请求头字段设置会话日志并将请求映射到会话：

```
session_log_zone /path/to/log format=combined
                    zone=one:1m timeout=30s
                    md5=$binary_remote_addr$http_user_agent;

location /media/ {
    session_log one;
}
```

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

### session\_log

| -       | 说明                                |
| ------- | --------------------------------- |
| **语法**  | **session\_log** `name` \| `off`; |
| **默认**  | session\_log off;                 |
| **上下文** | http、server、location              |

允许使用指定的会话日志。特殊值 `off` 取消从先前配置级别继承的所有 `session_log` 指令。

### session\_log\_format

| -       | 说明                                          |
| ------- | ------------------------------------------- |
| **语法**  | **session\_log\_format** `name string ...`; |
| **默认**  | session\_log\_format combined "...";        |
| **上下文** | http                                        |

指定日志的输出格式。`$body_bytes_sent` 变量的值聚合在会话的所有请求中。可用于记录的所有其他变量的值对应于会话中的第一个请求。

### session\_log\_zone

| -       | 说明                                                                                             |
| ------- | ---------------------------------------------------------------------------------------------- |
| **语法**  | **session\_log\_zone** `path zone=name:size [format=format] [timeout=time] [id=id] [md5=md5]`; |
| **默认**  | ——                                                                                             |
| **上下文** | http                                                                                           |

设置日志文件的路径，并配置用于存储当前活动会话的共享内存区域。

只要会话中的最后一个请求经过的时间不超过指定的 `timeout`（默认为 30 秒），会话就被视为活动状态。会话不再处于活动状态后将被写入日志。

`id` 参数标识请求映射到的会话。`id` 参数设置为 MD5 哈希的十六进制形式（例如，使用变量从 cookie 中获取）。如果未指定此参数或不是有效的 MD5 哈希，则 nginx 将根据 `md5` 参数的值计算 MD5 哈希，并使用此哈希创建新会话。`id` 和 `md5` 参数都可以包含变量。

`format` 参数设置 [session\_log\_format](/nginx-docs/he-xin-gong-neng/http/ngx_http_session_log_module.md#session_log_format) 指令配置的自定义会话日志格式。如果未指定 `format`，则使用预定义的 `combined`格式。

## 内嵌变量 <a href="#embedded_variables" id="embedded_variables"></a>

`ngx_http_session_log_module` 模块支持两个内嵌变量：

* `$session_log_id`

  当前会话 ID
* `$session_log_binary_id`

  二进制形式的当前会话 ID（16字节）。

## 原文档

* <http://nginx.org/en/docs/http/ngx_http_session_log_module.html>


---

# Agent Instructions: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
