2019-11-21 21:49:53 +08:00
<!--
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
-->
2020-08-12 22:54:11 +08:00
- [中文 ](../zh-cn/plugins/response-rewrite.md )
2019-11-21 21:49:53 +08:00
2019-11-24 21:15:39 +08:00
# Summary
2020-05-26 13:38:28 +08:00
2019-11-24 21:15:39 +08:00
- [**Name** ](#name )
- [**Attributes** ](#attributes )
- [**How To Enable** ](#how-to-enable )
- [**Test Plugin** ](#test-plugin )
- [**Disable Plugin** ](#disable-plugin )
2020-11-18 12:51:12 +08:00
- [**Attention** ](#Attention )
2019-11-24 21:15:39 +08:00
## Name
2020-05-26 13:38:28 +08:00
2020-11-18 12:51:12 +08:00
response rewrite plugin, rewrite the content returned by the upstream as well as Apache APISIX itself.
2019-11-21 21:49:53 +08:00
2020-12-13 00:04:48 +08:00
**scenario**:
2020-05-26 13:38:28 +08:00
2019-11-24 21:15:39 +08:00
1. can set `Access-Control-Allow-*` series field to support CORS(Cross-origin Resource Sharing).
2020-12-10 23:05:36 +08:00
2. we can set customized `status_code` and `Location` field in header to achieve redirect, you can also use [redirect ](redirect.md ) plugin if you just want a redirection.
2019-11-21 21:49:53 +08:00
2019-11-24 21:15:39 +08:00
## Attributes
2020-05-26 13:38:28 +08:00
2020-09-23 08:11:27 +08:00
| Name | Type | Requirement | Default | Valid | Description |
| ----------- | ------- | ----------- | ------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| status_code | integer | optional | | [200, 598] | New `status code` to client, keep the original response code by default. |
| body | string | optional | | | New `body` to client, and the content-length will be reset too. |
| body_base64 | boolean | optional | false | | Identify if `body` in configuration need base64 decoded before rewrite to client. |
| headers | object | optional | | | Set the new `headers` for client, can set up multiple. If it exists already from upstream, will rewrite the header, otherwise will add the header. You can set the corresponding value to an empty string to remove a header. |
2019-11-21 21:49:53 +08:00
2019-11-24 21:15:39 +08:00
## How To Enable
2020-05-26 13:38:28 +08:00
2019-11-21 21:49:53 +08:00
Here's an example, enable the `response rewrite` plugin on the specified route:
```shell
2020-11-28 19:05:14 +08:00
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
2019-11-21 21:49:53 +08:00
{
"methods": ["GET"],
"uri": "/test/index.html",
"plugins": {
"response-rewrite": {
"body": "{\"code\":\"ok\",\"message\":\"new json body\"}",
"headers": {
"X-Server-id": 3,
"X-Server-status": "on"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:80": 1
}
}
}'
```
2019-11-24 21:15:39 +08:00
## Test Plugin
2020-05-26 13:38:28 +08:00
2019-11-21 21:49:53 +08:00
Testing based on the above examples :
2019-11-24 21:15:39 +08:00
2019-11-21 21:49:53 +08:00
```shell
curl -X GET -i http://127.0.0.1:9080/test/index.html
```
2019-11-24 21:15:39 +08:00
It will output like below,no matter what kind of content from upstream.
2021-01-03 11:26:40 +08:00
2019-11-21 21:49:53 +08:00
```
2020-05-26 13:38:28 +08:00
2019-11-21 21:49:53 +08:00
HTTP/1.1 200 OK
Date: Sat, 16 Nov 2019 09:15:12 GMT
Transfer-Encoding: chunked
Connection: keep-alive
X-Server-id: 3
X-Server-status: on
{"code":"ok","message":"new json body"}
```
This means that the `response rewrite` plugin is in effect.
2019-11-24 21:15:39 +08:00
## Disable Plugin
2020-05-26 13:38:28 +08:00
2019-11-24 21:15:39 +08:00
When you want to disable the `response rewrite` plugin, it is very simple,
you can delete the corresponding json configuration in the plugin configuration,
no need to restart the service, it will take effect immediately:
2020-05-26 13:38:28 +08:00
2019-11-24 21:15:39 +08:00
```shell
2020-11-28 19:05:14 +08:00
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
2019-11-24 21:15:39 +08:00
{
"methods": ["GET"],
"uri": "/test/index.html",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:80": 1
}
}
}'
```
The `response rewrite` plugin has been disabled now. It works for other plugins.
2020-11-18 12:51:12 +08:00
## Attention
`ngx.exit` will interrupt the execution of the current request and return status code to Nginx.
![](https://cdn.jsdelivr.net/gh/Miss-you/img/picgo/20201113010623.png)
However, if you execute `ngx.exit` during the access phase, it only interrupts the request processing phase, and the response phase will still process it, i.e. if you configure the `response-rewrite` plugin, it will force overwriting of your response information (e.g. response status code).