proxy-rewrite

Description

The proxy-rewrite Plugin rewrites Upstream proxy information such as scheme, uri and host.

Attributes

NameTypeRequiredDefaultValid valuesDescription
uristringFalseNew Upstream forwarding address. Value supports Nginx variables. For example, $arg_name.
methodstringFalse[“GET”, “POST”, “PUT”, “HEAD”, “DELETE”, “OPTIONS”,”MKCOL”, “COPY”, “MOVE”, “PROPFIND”, “PROPFIND”,”LOCK”, “UNLOCK”, “PATCH”, “TRACE”]Rewrites the HTTP method.
regex_uriarray[string]FalseNew 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.
hoststringFalseNew Upstream host address.
headersobjectFalse
headers.addobjectfalseAppend the new headers. The format is {“name: value”,…}. The values in the header can contain Nginx variables like $remote_addr and $balancer_ip.
headers.setobjectfalseOverwrite 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.removearrayfalseRemove the headers. The format is [“name”, …].
use_real_request_uri_unsafebooleanFalsefalseUse 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 Priority

Header configurations are executed according to the following priorities:

add > remove > set

Enabling the Plugin

The example below enables the proxy-rewrite Plugin on a specific Route:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/test/index.html",
  5. "plugins": {
  6. "proxy-rewrite": {
  7. "uri": "/test/home.html",
  8. "host": "iresty.com",
  9. "headers": {
  10. "set": {
  11. "X-Api-Version": "v1",
  12. "X-Api-Engine": "apisix",
  13. "X-Api-useless": ""
  14. },
  15. "add": {
  16. "X-Request-ID": "112233"
  17. },
  18. "remove":[
  19. "X-test"
  20. ]
  21. }
  22. }
  23. },
  24. "upstream": {
  25. "type": "roundrobin",
  26. "nodes": {
  27. "127.0.0.1:80": 1
  28. }
  29. }
  30. }'

Example usage

Once you have enabled the Plugin as mentioned below, you can test the Route:

  1. 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:

  1. 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 Plugin

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

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/test/index.html",
  5. "plugins": {},
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:80": 1
  10. }
  11. }
  12. }'