Skip to content

Commit 1460edd

Browse files
authored
docs: improve proxy-mirror plugin docs (#11912)
1 parent 63a5348 commit 1460edd

File tree

2 files changed

+149
-162
lines changed

2 files changed

+149
-162
lines changed

docs/en/latest/plugins/proxy-mirror.md

+73-86
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ keywords:
44
- Apache APISIX
55
- API Gateway
66
- Proxy Mirror
7-
description: This document describes the information about the Apache APISIX proxy-mirror Plugin, you can use it to mirror the client requests.
7+
description: The proxy-mirror Plugin duplicates ingress traffic to APISIX and forwards them to a designated Upstream without interrupting the regular services.
88
---
9+
910
<!--
1011
#
1112
# Licensed to the Apache Software Foundation (ASF) under one or more
@@ -25,47 +26,48 @@ description: This document describes the information about the Apache APISIX pro
2526
#
2627
-->
2728

28-
## Description
29-
30-
The `proxy-mirror` Plugin can be used to mirror client requests. Traffic mirroring duplicates the real online traffic to the mirroring service. This enables specific analysis of the online traffic or request content without interrupting the online service.
29+
<head>
30+
<link rel="canonical" href="https://docs.api7.ai/hub/proxy-mirror" />
31+
</head>
3132

32-
:::note
33+
## Description
3334

34-
The response returned by the mirror request is ignored.
35+
The `proxy-mirror` Plugin duplicates ingress traffic to APISIX and forwards them to a designated upstream, without interrupting the regular services. You can configure the Plugin to mirror all traffic or only a portion. The mechanism benefits a few use cases, including troubleshooting, security inspection, analytics, and more.
3536

36-
:::
37+
Note that APISIX ignores any response from the Upstream host receiving mirrored traffic.
3738

3839
## Attributes
3940

4041
| Name | Type | Required | Default | Valid values | Description |
4142
|--------------|--------|----------|---------|--------------|---------------------------------------------------------------------------------------------------------------------------|
42-
| host | string | True | | | Address of the mirror service. It needs to contain the scheme (`http(s)` or `grpc(s)`) but without the path. For example, `http://127.0.0.1:9797`. |
43-
| path | string | False | | | Path of the mirror request. If unspecified, current path will be used. If it is for mirroring grpc traffic, this option is no longer applicable. |
44-
| path_concat_mode | string | False | replace | ["replace", "prefix"] | If the path of a mirror request is specified, set the concatenation mode of request paths. The `replace` mode will directly use `path` as the path of the mirror request. The `prefix` mode will use the `path` + `source request URI` as the path to the mirror request. If it is for mirroring grpc traffic, this option is no longer applicable too. |
45-
| sample_ratio | number | False | 1 | [0.00001, 1] | Ratio of the requests that will be mirrored. |
43+
| host | string | True | | | Address of the host to forward the mirrored traffic to. The address should contain the scheme but without the path, such as `http://127.0.0.1:8081`. |
44+
| path | string | False | | | Path of the host to forward the mirrored traffic to. If unspecified, default to the current URI path of the Route. Not applicable if the Plugin is mirroring gRPC traffic. |
45+
| path_concat_mode | string | False | replace | ["replace", "prefix"] | Concatenation mode when `path` is specified. When set to `replace`, the configured `path` would be directly used as the path of the host to forward the mirrored traffic to. When set to `prefix`, the path to forward to would be the configured `path`, appended by the requested URI path of the Route. Not applicable if the Plugin is mirroring gRPC traffic. |
46+
| sample_ratio | number | False | 1 | [0.00001, 1] | Ratio of the requests that will be mirrored. By default, all traffic are mirrored. |
4647

47-
You can customize the proxy timeouts for the mirrored sub-requests by configuring the `plugin_attr` key in your configuration file (`conf/config.yaml`). This can be used for mirroring traffic to a slow backend.
48+
## Static Configurations
4849

49-
```yaml title="conf/config.yaml"
50+
By default, timeout values for the Plugin are pre-configured in the [default configuration](https://github.com/apache/apisix/blob/master/apisix/cli/config.lua).
51+
52+
To customize these values, add the corresponding configurations to `config.yaml`. For example:
53+
54+
```yaml
5055
plugin_attr:
5156
proxy-mirror:
5257
timeout:
53-
connect: 2000ms
54-
read: 2000ms
55-
send: 2000ms
58+
connect: 60s
59+
read: 60s
60+
send: 60s
5661
```
5762
58-
| Name | Type | Default | Description |
59-
|---------|--------|---------|-------------------------------------------|
60-
| connect | string | 60s | Connect timeout to the mirrored Upstream. |
61-
| read | string | 60s | Read timeout to the mirrored Upstream. |
62-
| send | string | 60s | Send timeout to the mirrored Upstream. |
63+
Reload APISIX for changes to take effect.
6364
64-
## Enable Plugin
65+
## Examples
6566
66-
You can enable the Plugin on a specific Route as shown below:
67+
The examples below demonstrate how to configure `proxy-mirror` for different scenarios.
6768

6869
:::note
70+
6971
You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command:
7072

7173
```bash
@@ -74,85 +76,70 @@ admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"/
7476

7577
:::
7678

77-
```shell
78-
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
79-
-H "X-API-KEY: $admin_key" -X PUT -d '
80-
{
81-
"plugins": {
82-
"proxy-mirror": {
83-
"host": "http://127.0.0.1:9797"
84-
}
85-
},
86-
"upstream": {
87-
"nodes": {
88-
"127.0.0.1:1999": 1
89-
},
90-
"type": "roundrobin"
91-
},
92-
"uri": "/hello"
93-
}'
94-
```
95-
96-
### Specify the timeout for mirror subrequests
79+
### Mirror Partial Traffic
9780

98-
We can specify the `timeout` for subrequests in `plugin_attr` in `conf/config.yaml`. This is useful in connection reuse scenarios when mirroring traffic to a very slow backend service.
81+
The following example demonstrates how you can configure `proxy-mirror` to mirror 50% of the traffic to a Route and forward them to another Upstream service.
9982

100-
Since mirror requests are implemented as sub-requests, delays in sub-requests will block the original request until the sub-requests are completed. So you can configure the timeout time to protect the sub-requests from excessive delays that affect the original requests.
83+
Start a sample NGINX server for receiving mirrored traffic:
10184

102-
| Name | Type | Default | Description |
103-
| --- | --- | --- | --- |
104-
| connect | string | 60s | Connection timeout for mirror request to upstream. |
105-
| read | string | 60s | The time that APISIX maintains the connection with the mirror server; if APISIX does not receive a response from the mirror server within this time, the connection is closed. |
106-
| send | string | 60s | The time that APISIX maintains the connection with the mirror server; if APISIX does not send a request within this time, the connection is closed. |
107-
108-
```yaml
109-
plugin_attr:
110-
proxy-mirror:
111-
timeout:
112-
connect: 2000ms
113-
read: 2000ms
114-
send: 2000ms
85+
```shell
86+
docker run -p 8081:80 --name nginx nginx
11587
```
11688

117-
## Example usage
118-
119-
:::tip
89+
You should see NGINX access log and error log on the terminal session.
12090

121-
For testing you can create a test server by running:
91+
Open a new terminal session and create a Route with `proxy-mirror` to mirror 50% of the traffic:
12292

12393
```shell
124-
python -m http.server 9797
94+
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
95+
-H "X-API-KEY: ${admin_key}" \
96+
-d '{
97+
"id": "traffic-mirror-route",
98+
"uri": "/get",
99+
"plugins": {
100+
"proxy-mirror": {
101+
"host": "http://127.0.0.1:8081",
102+
"sample_ratio": 0.5
103+
}
104+
},
105+
"upstream": {
106+
"nodes": {
107+
"httpbin.org": 1
108+
},
109+
"type": "roundrobin"
110+
}
111+
}'
125112
```
126113

127-
:::
128-
129-
Once you have configured the Plugin as shown above, the requests made will be mirrored to the configured host.
114+
Send Generate a few requests to the Route:
130115

131116
```shell
132-
curl http://127.0.0.1:9080/hello -i
117+
curl -i "http://127.0.0.1:9080/get"
133118
```
134119

135-
```shell
136-
HTTP/1.1 200 OK
137-
...
138-
hello world
120+
You should receive `HTTP/1.1 200 OK` responses for all requests.
121+
122+
Navigating back to the NGINX terminal session, you should see a number of access log entries, roughly half the number of requests generated:
123+
124+
```text
125+
172.17.0.1 - - [29/Jan/2024:23:11:01 +0000] "GET /get HTTP/1.1" 404 153 "-" "curl/7.64.1" "-"
139126
```
140127

141-
## Delete Plugin
128+
This suggests APISIX has mirrored the request to the NGINX server. Here, the HTTP response status is `404` since the sample NGINX server does not implement the Route.
142129

143-
To remove the `proxy-mirror` Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.
130+
### Configure Mirroring Timeouts
144131

145-
```shell
146-
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
147-
-H "X-API-KEY: $admin_key" -X PUT -d '
148-
{
149-
"uri": "/hello",
150-
"plugins": {},
151-
"upstream": {
152-
"type": "roundrobin",
153-
"nodes": {
154-
"127.0.0.1:1999": 1
155-
}
156-
}
157-
}'
132+
The following example demonstrates how you can update the default connect, read, and send timeouts for the Plugin. This could be useful when mirroring traffic to a very slow backend service.
133+
134+
As the request mirroring was implemented as sub-requests, excessive delays in the sub-requests could lead to the blocking of the original requests. By default, the connect, read, and send timeouts are set to 60 seconds. To update these values, you can configure them in the `plugin_attr` section of the configuration file as such:
135+
136+
```yaml title="conf/config.yaml"
137+
plugin_attr:
138+
proxy-mirror:
139+
timeout:
140+
connect: 2000ms
141+
read: 2000ms
142+
send: 2000ms
158143
```
144+
145+
Reload APISIX for changes to take effect.

0 commit comments

Comments
 (0)