Skip to content

Commit

Permalink
docs: add re case on response-rewrite plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
zhendongcmss committed Jun 6, 2022
1 parent 22e23ba commit 795a2f8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
41 changes: 40 additions & 1 deletion docs/en/latest/plugins/response-rewrite.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ You can also use the [redirect](./redirect.md) Plugin to setup redirects.
| Name | Type | Required | Default | Valid values | Description |
|-------------|---------|----------|---------|---------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| status_code | integer | False | | [200, 598] | New HTTP status code in the response. If unset, falls back to the original status code. |
| body | string | False | | | New body of the response. The content-length would also be reset. |
| body | string | False | | | New body of the response. The content-length would also be reset. If body and filters configured at the same time, body configuration will not work. |
| body_base64 | boolean | False | false | | When set, the body of the request will be decoded before writing to the client. |
| headers | object | False | | | New headers for the response. Headers are overwritten if they are present in the Upstream response otherwise, they are added to the Upstream headers. To remove a header, set the header value to an empty string. The values in the header can contain Nginx variables like `$remote_addr` and `$balancer_ip`. |
| vars | array[] | False | | See [lua-resty-expr](https://github.com/api7/lua-resty-expr#operator-list) for a list of available operators. | Nginx variable expressions to conditionally execute the rewrite. The Plugin will be executed unconditionally if this value is empty. |
Expand Down Expand Up @@ -118,6 +118,45 @@ X-Server-balancer_addr: 127.0.0.1:80
{"code":"ok","message":"new json body"}
```

Replace the string client_addr of body to client_addr_replace.

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"response-rewrite": {
"headers": {
"X-Server-id": 3,
"X-Server-status": "on",
"X-Server-balancer_addr": "$balancer_ip:$balancer_port"
},
"filters": [{"regex": "client_addr", "scope": "global", "replace": "client_addr_replace"}],
"vars":[
[ "status","==",200 ]
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8881": 1
}
},
"uri": "/*"
}'
```

```shell
HTTP/1.1 200 OK
Transfer-Encoding: chunked
X-Server-status: on
X-Server-balancer-addr: 127.0.0.1:8881
X-Server-id: 3

{"method": "GET", "time": "2022-06-06 07:26:40.017836", "client_addr_replace": "::ffff:127.0.0.1"}

```

:::info IMPORTANT

[ngx.exit](https://openresty-reference.readthedocs.io/en/latest/Lua_Nginx_API/#ngxexit) will interrupt the execution of a request and returns its status code to Nginx.
Expand Down
41 changes: 40 additions & 1 deletion docs/zh/latest/plugins/response-rewrite.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ description: 本文介绍了关于 Apache APISIX `response-rewrite` 插件的基
| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
|-----------------|---------|-----|--------|-----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| status_code | integer || | [200, 598] | 修改上游返回状态码,默认保留原始响应代码。 |
| body | string || | | 修改上游返回的 `body` 内容,如果设置了新内容,header 里面的 content-length 字段也会被去掉。 |
| body | string || | | 修改上游返回的 `body` 内容,如果设置了新内容,header 里面的 content-length 字段也会被去掉。`body``filters` 同时配置时 `body` 不会生效。 |
| body_base64 | boolean || false | | 描述 `body` 字段是否需要 base64 解码之后再返回给客户端,用在某些图片和 Protobuffer 场景。 |
| headers | object || | | 返回给客户端的 `headers`,这里可以设置多个。头信息如果存在将重写,不存在则添加。想要删除某个 header 的话,把对应的值设置为空字符串即可。这个值能够以 `$var` 的格式包含 NGINX 变量,比如 `$remote_addr $balancer_ip`|
| vars | array[] || | | `vars` 是一个表达式列表,只有满足条件的请求和响应才会修改 body 和 header 信息,来自 [lua-resty-expr](https://github.com/api7/lua-resty-expr#operator-list)。如果 `vars` 字段为空,那么所有的重写动作都会被无条件的执行。 |
Expand Down Expand Up @@ -119,6 +119,45 @@ X-Server-balancer_addr: 127.0.0.1:80
{"code":"ok","message":"new json body"}
```

使用正则匹配将返回body的client_addr替换为client_addr_replace

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"response-rewrite": {
"headers": {
"X-Server-id": 3,
"X-Server-status": "on",
"X-Server-balancer_addr": "$balancer_ip:$balancer_port"
},
"filters": [{"regex": "client_addr", "scope": "global", "replace": "client_addr_replace"}],
"vars":[
[ "status","==",200 ]
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8881": 1
}
},
"uri": "/*"
}'
```

```shell
HTTP/1.1 200 OK
Transfer-Encoding: chunked
X-Server-status: on
X-Server-balancer-addr: 127.0.0.1:8881
X-Server-id: 3

{"method": "GET", "time": "2022-06-06 07:26:40.017836", "client_addr_replace": "::ffff:127.0.0.1"}

```

:::info IMPORTANT

[ngx.exit](https://openresty-reference.readthedocs.io/en/latest/Lua_Nginx_API/#ngxexit) 将会中断当前请求的执行并将其状态码返回给 NGINX。
Expand Down

0 comments on commit 795a2f8

Please sign in to comment.