# ngx\_http\_mirror\_module

* [示例配置](#example_configuration)
* [指令](#directives)
  * [mirror](#mirror)
  * [mirror\_request\_body](#mirror_request_body)

`ngx_http_mirror_module` 模块（1.13.4）通过创建后台镜像子请求来实现原始请求的镜像。镜像子请求的响应将被忽略。

> 译者注：利用 mirror 模块，业务可以将线上实时访问流量拷贝至其他环境，基于这些流量可以做版本发布前的预先验证，进行流量放大后的压测等等。

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

```
location / {
    mirror /mirror;
    proxy_pass http://backend;
}

location /mirror {
    internal;
    proxy_pass http://test_backend$request_uri;
}
```

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

### mirror

|       - | 说明                         |
| ------: | -------------------------- |
|  **语法** | **mirror** `uri` \| `off`; |
|  **默认** | mirror off;                |
| **上下文** | http、server、location       |

设置将做成镜像的原始请求的 URI。可以在同一层级上指定多个镜像（译者注: 多次重复一个镜像可以实现流量放大）。

### mirror\_request\_body

|       - | 说明                                       |
| ------: | ---------------------------------------- |
|  **语法** | **mirror\_request\_body** `on` \| `off`; |
|  **默认** | mirror\_request\_body on;                |
| **上下文** | http、server、location                     |

指示是否将客户端请求体做成镜像。启用后，将在创建镜像子请求之前读取客户端请求体。在这种情况下，将禁用由 [proxy\_request\_buffering](https://docshome.gitbook.io/nginx-docs/he-xin-gong-neng/ngx_http_proxy_module#proxy_request_buffering)、[fastcgi\_request\_buffering](https://docshome.gitbook.io/nginx-docs/he-xin-gong-neng/ngx_http_fastcgi_module#fastcgi_request_buffering)、[scgi\_request\_buffering](https://docshome.gitbook.io/nginx-docs/he-xin-gong-neng/ngx_http_scgi_module#scgi_request_buffering) 和 [uwsgi\_request\_buffering](https://docshome.gitbook.io/nginx-docs/he-xin-gong-neng/ngx_http_uwsgi_module#uwsgi_request_buffering) 指令设置的未缓冲的客户端请求正代理。

```
location / {
    mirror /mirror;
    mirror_request_body off;
    proxy_pass http://backend;
}

location /mirror {
    internal;
    proxy_pass http://log_backend;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
}
```

## 原文档

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