proxy-rewrite
#
DescriptionThe proxy-rewrite
Plugin rewrites Upstream proxy information such as scheme
, uri
and host
.
#
AttributesName | Type | Required | Default | Valid values | Description |
---|---|---|---|---|---|
uri | string | False | New Upstream forwarding address. Value supports Nginx variables. For example, $arg_name . | ||
method | string | False | ["GET", "POST", "PUT", "HEAD", "DELETE", "OPTIONS","MKCOL", "COPY", "MOVE", "PROPFIND", "PROPFIND","LOCK", "UNLOCK", "PATCH", "TRACE"] | Rewrites the HTTP method. | |
regex_uri | array[string] | False | New upstream forwarding address. Regular expressions can be used to match the URL from client. If it matches, the URL template is forwarded to the Upstream otherwise, the URL from the client is forwarded. When both uri and regex_uri are configured, uri is used first. For example, [" ^/iresty/(.*)/(.*)/(.*)", "/$1-$2-$3"] . Here, the first element is the regular expression to match and the second element is the URL template forwarded to the Upstream. | ||
host | string | False | New Upstream host address. | ||
headers | object | False | |||
headers.add | object | false | Append the new headers. The format is {"name: value",...} . The values in the header can contain Nginx variables like $remote_addr and $balancer_ip. | ||
headers.set | object | false | Overwrite the headers. If header is not exist, will add it. The format is {"name": "value", ...} . The values in the header can contain Nginx variables like $remote_addr and $balancer_ip. | ||
headers.remove | array | false | Remove the headers. The format is ["name", ...] . | ||
use_real_request_uri_unsafe | boolean | False | false | Use real_request_uri (original $request_uri in nginx) to bypass URI normalization. Enabling this is considered unsafe as it bypasses all URI normalization steps. |
#
Header PriorityHeader configurations are executed according to the following priorities:
add
> remove
> set
#
Enabling the PluginThe example below enables the proxy-rewrite
Plugin on a specific Route:
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"methods": ["GET"],
"uri": "/test/index.html",
"plugins": {
"proxy-rewrite": {
"uri": "/test/home.html",
"host": "iresty.com",
"headers": {
"set": {
"X-Api-Version": "v1",
"X-Api-Engine": "apisix",
"X-Api-useless": ""
},
"add": {
"X-Request-ID": "112233"
},
"remove":[
"X-test"
]
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:80": 1
}
}
}'
#
Example usageOnce you have enabled the Plugin as mentioned below, you can test the Route:
curl -X GET http://127.0.0.1:9080/test/index.html
Once you send the request, you can check the Upstream access.log
for its output:
127.0.0.1 - [26/Sep/2019:10:52:20 +0800] iresty.com GET /test/home.html HTTP/1.1 200 38 - curl/7.29.0 - 0.000 199 107
#
Disable PluginTo disable the proxy-rewrite
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.
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"methods": ["GET"],
"uri": "/test/index.html",
"plugins": {},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:80": 1
}
}
}'