Skip to main content
Version: 2.11




response rewrite plugin, rewrite the content returned by the upstream as well as Apache APISIX itself.


  1. can set Access-Control-Allow-* series field to support CORS(Cross-origin Resource Sharing).
  2. we can set customized status_code and Location field in header to achieve redirect, you can also use redirect plugin if you just want a redirection.


status_codeintegeroptional[200, 598]New status code to client, keep the original response code by default.
bodystringoptionalNew body to client, and the content-length will be reset too.
body_base64booleanoptionalfalseIdentify if body in configuration need base64 decoded before rewrite to client.
headersobjectoptionalSet 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. The value can contain Nginx variables in $var format, like $remote_addr $balancer_ip
varsarray[]optionalA DSL to evaluate with the given ngx.var. See vars lua-resty-expr. if the vars is empty, then all rewrite operations will be executed unconditionally

How To Enable#

Here's an example, enable the response-rewrite plugin on the specified route:

curl  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "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",                "X-Server-balancer_addr": "$balancer_ip:$balancer_port"            },            "vars":[                [ "status","==",200 ]            ]        }    },    "upstream": {        "type": "roundrobin",        "nodes": {            "": 1        }    }}'

Test Plugin#

Testing based on the above examples :

curl -X GET -i

It will output like below,no matter what kind of content from upstream, the vars will make sure that only rewrite response that http status is 200.

HTTP/1.1 200 OKDate: Sat, 16 Nov 2019 09:15:12 GMTTransfer-Encoding: chunkedConnection: keep-aliveX-Server-id: 3X-Server-status: onX-Server-balancer_addr:
{"code":"ok","message":"new json body"}

This means that the response-rewrite plugin is in effect.

Disable Plugin#

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:

curl  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "methods": ["GET"],    "uri": "/test/index.html",    "upstream": {        "type": "roundrobin",        "nodes": {            "": 1        }    }}'

The response-rewrite plugin has been disabled now. It works for other plugins.


ngx.exit will interrupt the execution of the current request and return status code to Nginx.

ngx.edit tabular overview

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).