github.com/nginxinc/kubernetes-ingress@v1.12.5/examples-of-custom-resources/rewrites/README.md (about) 1 # Rewrites Support 2 3 You can configure NGINX to rewrite the URI of a request before sending it to the application. For example, `/tea/green` can be rewritten to `/green`. 4 5 To configure URI rewriting you need to use the [ActionProxy](https://docs.nginx.com/nginx-ingress-controller/configuration/virtualserver-and-virtualserverroute-resources/#action-proxy) of the [VirtualServer or VirtualServerRoute](https://docs.nginx.com/nginx-ingress-controller/configuration/virtualserver-and-virtualserverroute-resources/). 6 7 ## Example with a Prefix Path 8 9 In the following example we load balance two applications that require URI rewriting using prefix-based URI matching: 10 11 ```yaml 12 apiVersion: k8s.nginx.org/v1 13 kind: VirtualServer 14 metadata: 15 name: cafe 16 spec: 17 host: cafe.example.com 18 upstreams: 19 - name: tea 20 service: tea-svc 21 port: 80 22 - name: coffee 23 service: coffee-svc 24 port: 80 25 routes: 26 - path: /tea/ 27 action: 28 proxy: 29 upstream: tea 30 rewritePath: / 31 - path: /coffee 32 action: 33 proxy: 34 upstream: coffee 35 rewritePath: /beans 36 ``` 37 38 Below are the examples of how the URI of requests to the *tea-svc* are rewritten (Note that the `/tea` requests are redirected to `/tea/`). 39 * `/tea/` -> `/` 40 * `/tea/abc` -> `/abc` 41 42 Below are the examples of how the URI of requests to the *coffee-svc* are rewritten. 43 * `/coffee` -> `/beans` 44 * `/coffee/` -> `/beans/` 45 * `/coffee/abc` -> `/beans/abc` 46 47 ## Example with Regular Expressions 48 49 If the route path is a regular expression instead of a prefix or an exact match, the `rewritePath` can include capture groups with `$1-9`, for example: 50 51 ```yaml 52 apiVersion: k8s.nginx.org/v1 53 kind: VirtualServer 54 metadata: 55 name: cafe 56 spec: 57 host: cafe.example.com 58 upstreams: 59 - name: tea 60 service: tea-svc 61 port: 80 62 routes: 63 - path: ~ /tea/?(.*) 64 action: 65 proxy: 66 upstream: tea 67 rewritePath: /$1 68 ``` 69 70 Note the capture group in the path `(.*)` is used in the rewritePath `/$1`. This is needed in order to pass the rest of the request URI (after `/tea`). 71 72 Below are the examples of how the URI of requests to the *tea-svc* are rewritten. 73 * `/tea` -> `/` 74 * `/tea/` -> `/` 75 * `/tea/abc` -> `/abc`